我有一个简单的QObject
:
class Engine : public QObject
{
Q_OBJECT
public:
explicit Engine(QObject* parent = 0);
signals:
void finished();
public slots:
void start();
};
一个实例 Engine* engine
存储在主窗口类中。当按下按钮时,会发生以下情况:
QThread* thread = new QThread;
engine->moveToThread(thread);
connect(engine, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
connect(thread, SIGNAL(started()), engine, SLOT(start()));
connect(engine, SIGNAL(finished()), thread, SLOT(quit()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
thread->start();
我的问题是,当 thread
完成后,engine
会发生什么?我能创建另一个线程并将 engine
移动到那个线程中,再重复一遍吗?
Engine
移动到另一个线程时,(再次)会出现错误提示“无法移动到目标线程”。我应该如何从之前的线程中断开Engine
? - Henricus V.Engine
所在的线程中调用 moveToThread 方法。第一次时,它仍在主线程中,所以不会出现任何错误。第二次需要将其从当前所在的thread
中移动。请参见 moveToThread 文档中本节底部的警告。另外,阅读 线程亲和性 也会很有用。 - Punitto Moe