考虑QObject
:
class TestObject: public QObject
{
Q_OBJECT
public slots:
void doStuff();
};
我们将在一些不同的线程中运行此对象:
TestObject* o = new TestObject;
o->moveToThread(someThreadPointer);
现在,如何调用doStuff
以便它在工作线程中发生?如果我在主线程中有其他对象,我可以这样做:
class OtherObject: public QObject
{
Q_OBJECT
signals:
signalToDoStuff();
};
在主线程中:
TestObject* o = new TestObject;
o->moveToThread(someThreadPointer);
OtherObject other* = new OtherObject;
QObject::connect(other, &OtherObject::signalToDoStuff, o, &TestObject::doStuff);
// Emit the signal which causes `doStuff` to be called in other thread
other->signalToDoStuff();
但这还有很长的路要走。我需要排队一个要调用doStuff
的事件。