C++ (Qt) void Thread::run (){ QEventloop loop; QTimer timer; connect(&timer,SIGNAL(timeout()),this,SLOT(createEvent()),Qt::DirectConnection); timer.start(defaultTime); loop.exec(); } void Thread::createEvent(){ Event *event = new Event(); QApplication::postEvent(objReceiver,event);} void Thread::setObjRecever(QObject * obj){ objReceiver = obj;} //___________________________________________ static const int my_type = 1099;class Event : public QEvent { public: Event() : QEvent((QEvent::Type)my_type){} void incCounter(int *var){ (*var)++;}} //____________________________________________ MainWindow:: MainWindow(QWidget*parent) :QMainWindow(parent), ui(new Ui:: MainWindow){ counter =0; thread = new Thread(); thread->setObjRecever(this); thread->start();} void MainWindow::customEvent(QEvent *e){ if(event->type() == my_type){ Event *event = staic_cast<Event*>(e); event->incCounter(&counter); ui->lcdNumber->display(counter); } QMainWindow:: customEvent(e);}
C++ (Qt)void MainWindow::customEvent(QEvent *e){ if(event->type() == my_type){ Event *event = staic_cast<Event*>(e); if ((counter % 1000) == 0) qDebug() << counter << QTime::currentTime(); event->incCounter(&counter);// ui->lcdNumber->display(counter); } QMainWindow:: customEvent(e);}