qApp->processEvents();
MyWidget::startTimer(){ time.start(); // QTime time in declaration timer->start( 1000 );}void MyWidget::timerSlot(){ label->setNum( time.elapsed() );}
#include <QtGui/QApplication>#include <QLCDNumber>#include <QThread>#include <QEvent>#include <QTime>#include <QMouseEvent>enum EventType { EVENT_UPDATE = QEvent::User + 1};class UpdateEvent : public QEvent{public: UpdateEvent(int num) : QEvent((QEvent::Type) EVENT_UPDATE), number(num) {;} int number;};class UpdateThread : public QThread{public: UpdateThread(QObject* o) : QThread(o), receiver(o) {;} virtual void run() { time.start(); while(true) { UpdateEvent* event = new UpdateEvent(time.elapsed()); QApplication::postEvent(receiver, event); msleep(1); } }private: QTime time; QObject* receiver;};class QTestWidget : public QLCDNumber{public: QTestWidget(QLCDNumber* w = NULL) : QLCDNumber(w) {;}protected: void mousePressEvent(QMouseEvent* e) { QApplication::sendPostedEvents(this, EVENT_UPDATE); } bool event(QEvent* e) { if(e->type() == EVENT_UPDATE) { UpdateEvent* event = (UpdateEvent*) e; display(event->number); return true; } else { QApplication::sendPostedEvents(this, EVENT_UPDATE); } return QLCDNumber::event(e); }};int main(int argc, char *argv[]){ QApplication a(argc, argv); QTestWidget w; w.show(); UpdateThread t(&w); t.start(); return a.exec();}