说明:在多线程程序中,线程分为主线程(main thread)和工作线程(worker thread)
#include
class Thread : public QThread
{
private:
void run()
{
qDebug()<<"From worker thread: "<() ;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"From main thread: "<<QThread::currentThreadId();
Thread t;
QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));
t.start();
return a.exec();
}
输出结果大致如下:
From main thread: 0x15a8
From worker thread: 0x128c
#if QT_VERSION>=0x050000
#include
#else
#include
#endif
class Thread : public QThread
{
Q_OBJECT
public:
Thread():m_stop(false)
{}
public slots:
void stop()
{
qDebug()<<"Thread::stop called from main thread: "<true;
}
private:
QMutex m_mutex;
bool m_stop;
void run()
{
qDebug()<<"From worker thread: "<while (1) {
{
QMutexLocker locker(&m_mutex);
if (m_stop) break;
}
msleep(10);
}
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"From main thread: "<"Stop Thread");
Thread t;
QObject::connect(&btn, SIGNAL(clicked()), &t, SLOT(stop()));
QObject::connect(&t, SIGNAL(finished()), &a, SLOT(quit()));
t.start();
btn.show();
return a.exec();
}
输出结果大致如下:
From main thread: 0x13a8
From worker thread: 0xab8
Thread::stop called from main thread: 0x13a8
#include
class Thread : public QThread
{
Q_OBJECT
private slots:
void onTimeout()
{
qDebug()<<"Thread::onTimeout get called from? : "<private:
void run()
{
qDebug()<<"From worker thread: "<this, SLOT(onTimeout()));
timer.start(1000);
exec();
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"From main thread: "<return a.exec();
}
输出结果大致如下:
From main thread: 0x13a4
From worker thread: 0x1330
Thread::onTimeout get called from?: 0x13a4
Thread::onTimeout get called from?: 0x13a4
Thread::onTimeout get called from?: 0x13a4
class Worker : public QObject
{
Q_OBJECT
private slots:
void onTimeout()
{
qDebug()<<"Worker::onTimeout get called from?: "<public QThread
{
Q_OBJECT
private:
void run()
{
qDebug()<<"From work thread: "<1000);
exec();
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"From main thread: "<return a.exec();
}
运行结果如下:
From main thread: 0x810
From work thread: 0xfac
Worker::onTimeout get called from?: 0xfac
Worker::onTimeout get called from?: 0xfac
Worker::onTimeout get called from?: 0xfac
#include
class Worker : public QObject
{
Q_OBJECT
private slots:
void onTimeout()
{
qDebug()<<"Worker::onTimeout get called from?: "<<QThread::currentThreadId();
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
qDebug()<<"From main thread: "<<QThread::currentThreadId();
QThread t;
QTimer timer;
Worker worker;
QObject::connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
timer.start(1000);
timer.moveToThread(&t);//这句代码不是必要的,不影响运行结果
worker.moveToThread(&t);
t.start();
return a.exec();
}
运行结果如下:
From main thread: 0x1310
Worker::onTimeout get called from?: 0x121c
Worker::onTimeout get called from?: 0x121c
Worker::onTimeout get called from?: 0x121c
void QCoreApplication::postEvent(QObject *receiver, QEvent *event)
{
QThreadData * volatile * pdata = &receiver->d_func()->threadData;
QThreadData *data = *pdata;
QMutexLocker locker(&data->postEventList.mutex);
data->postEventList.addEvent(QPostEvent(receiver, event));
}
实例如下:
#if QT_VERSION>=0x050000
#include
#else
#include
#endif
class Test : public QObject
{
Q_OBJECT
protected:
bool event(QEvent *evt)
{
if (evt->type() == QEvent::User) {
qDebug()<<"Event received in thread"<return true;
}
return QObject::event(evt);
}
};
class Button : public QPushButton
{
Q_OBJECT
Test *m_test;
public:
Button(Test *test):QPushButton("Send Event"), m_test(test)
{
connect(this, SIGNAL(clicked()), SLOT(onClicked()));
}
private slots:
void onClicked()
{
QCoreApplication::postEvent(m_test, new QEvent(QEvent::User));
}
};
#include "main.moc"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"From main thread: "<return a.exec();
}
运行结果如下:
From main thread: QThread(0x9e8100)
Event received in thread QThread(0x9e8100)
Event received in thread QThread(0x9e8100)
添加以下三句代码,结果就会不同
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug()<<"From main thread: "<//new line
test.moveToThread(&thread); //new line
thread.start(); //new line
Button btn(&test);
btn.show();
return a.exec();
}
运行结果如下:
From main thread: QThread(0x9e8100)
Event received in thread QThread(0x13fed4)
Event received in thread QThread(0x13fed4)