如何在不使用connect的情况下为另一个线程中的对象调用插槽?

时间:2016-08-02 23:02:57

标签: c++ multithreading qt signals-slots qthread

考虑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的事件。

0 个答案:

没有答案