安庆师范学院计算机与信息学院
Anqing teacher school College of computer and information
classBellEvent:publicQEvent {
public:
BellEvent(constQString&visitor,constQStringList&gifts); QStringm_visitor; QStringListm_gifts;
staticTypeeventType(); protected:
staticTypem_evType; };
classBellThread; classQTimer;
classRinger:publicQObject {
Q_OBJECT public:
Ringer(BellThread*t); publicslots:
voidonTimeout(); private:
BellThread*m_thread; };
classBellThread:publicQThread {
friendclassRinger; public:
BellThread(QObject*receiver);
protected: voidrun();
private:
QPointer
classMaster:publicQWidget {
Q_OBJECT
public:
第3页共61页
面向对象程序设计:实验报告
安庆师范学院计算机与信息学院
Anqing teacher school College of computer and information
Master(QWidget*parent=0); ~Master(); protected:
boolevent(QEvent*e);
private:
QLabel*m_visitorLabel; QLabel*m_movie; };
#endif//DOORBELL_H
doorbell.cpp
#include\ #include
#include
#include
QEvent::TypeBellEvent::m_evType=QEvent::None; BellEvent::BellEvent(constQString&visitor, constQStringList&gifts)
:QEvent(eventType()),m_visitor(visitor) ,m_gifts(gifts) { }
QEvent::TypeBellEvent::eventType() {
if(m_evType==QEvent::None) {
m_evType=(QEvent::Type)registerEventType(); }
returnm_evType; } //
BellThread::BellThread(QObject*receiver) :m_receiver(receiver) {
第4页共61页
面向对象程序设计:实验报告
安庆师范学院计算机与信息学院
Anqing teacher school College of computer and information
}
voidBellThread::run() {
qDebug()<<\ < QScopedPointer constchar*g_vistors[5]={ \,\,\, \,\ }; constchar*g_gifts[7]={ \,\,\, \,\,\, \ }; Ringer::Ringer(BellThread*t):m_thread(t) { QTimer::singleShot(3000,this,SLOT(onTimeout())); } voidRinger::onTimeout() { if(m_thread->m_receiver.isNull()) { m_thread->quit(); return; } qsrand(QDateTime::currentDateTime().toTime_t()); QStringListgifts; gifts< QTimer::singleShot((qrand()%8+2)*1000 第5页共61页 面向对象程序设计:实验报告 安庆师范学院计算机与信息学院 Anqing teacher school College of computer and information ,this,SLOT(onTimeout())); } Master::Master(QWidget*parent) :QWidget(parent),m_visitorLabel(newQLabel) ,m_movie(newQLabel) { BellThread*t=newBellThread(this); connect(t,SIGNAL(finished()),t,SLOT(deleteLater())); t->start(); QVBoxLayout*layout=newQVBoxLayout; layout->addWidget(m_visitorLabel); QMovie*gif=newQMovie(\); m_movie->setMovie(gif); layout->addWidget(m_movie); m_movie->setVisible(false); layout->addStretch(1); setLayout(layout); } Master::~Master() { } boolMaster::event(QEvent*e) { if(e->type()==BellEvent::eventType()) { BellEvent*ev=(BellEvent*)e; QStringstr=QString(\) .arg(ev->m_visitor) .arg(ev->m_gifts.join(\)); m_visitorLabel->setText(str); if(ev->m_gifts.contains(\) ||ev->m_gifts.contains(\)) { m_movie->setVisible(true); m_movie->movie()->start(); } else 第6页共61页 面向对象程序设计:实验报告 安庆师范学院计算机与信息学院 Anqing teacher school College of computer and information { m_movie->setVisible(false); m_movie->movie()->stop(); } returntrue; } returnQWidget::event(e); } doorbell.qrc 六、实验结果与分析 七、教师评价 第7页共61页 面向对象程序设计:实验报告