void sleep(int msec) { QTime tt; tt.start(); while(tt.elapsed() < msec);}int main(int argc, char ** argv) { qDebug() << "main started";// QApplication app(argc, argv); MyThread t; t.start(); sleep(100); emit t.setI(5); sleep(100); emit t.setI(10); sleep(100); emit t.setI(200); sleep(100); while(!t.isFinished()) { sleep(100); } qDebug() << "main finished"; return 0;}
#ifndef MYTHREAD_H#define MYTHREAD_H#include <QThread>#include <QEventLoop>class MyThread : public QThread { Q_OBJECT int i; QEventLoop eventLoop;public: MyThread();protected: void run();public slots: void setI(int i);};#endif // MYTHREAD_H
#include "MyThread.h"#include <QDebug>MyThread::MyThread() { i=0;}void MyThread::run() { qDebug() << "thread started"; while(i<100) { qDebug() << i; eventLoop.processEvents(); } qDebug() << "thread finished";}void MyThread::setI(int newI) { i = newI;}
main finished
moveToThread(this)