class A:public QObject
{
Q_OBJECT
public slots:
void f() {
while(1) {
qDebug()<<"f"<<thread()<<thread()->isRunning();
sleep(1);
**QMetaObject::invokeMethod(thread(), "quit", Qt::QueuedConnection);**
}
}
public slots:
void g() { qDebug() << "g"; }
};
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QThread th;
A a;
a.moveToThread(&th);
th.start();
a.f();// running in main thread
return app.exec();
}
输出始终是:为什么qthread永不放弃?
˚F的QThread(0xbfdef1e0)真
˚F的QThread(0xbfdef1e0)真
˚F的QThread(0xbfdef1e0)真
我想知道为什么的QThread从未退出,因为我使用“QMetaObject :: invokeMethod(thread(),”quit“,Qt :: QueuedConnection);”
感谢
a.f()在主线程中运行的不是子线程,子线程可以处理事件队列。 – camino
我已经更新了答案以澄清 – Chris