PDA

View Full Version : Calling a slot from another thread



pherthyl
19th March 2010, 21:23
Hey everyone, it's friday and I'm being dense so I came here for some help.

I have two threads, one is my interface to Skype (through their API), and one is the GUI thread. A lot of the Skype API functions are blocking, thus the separate thread is necessary.

However there is a lot of communication necessary between the two threads, mostly the GUI sending commands to the Skype thread to do things (call, answer, send message, log in, etc).

So far to send a command I just can just create a signal in the GUI class, like "doCall" which gets hooked up to the "call" slot in the Skype thread. Due to the magic of queued connections, this works just fine.

However it seems a bit cumbersome to have to define a signal for every command I want to send to the Skype thread.

Is there a way I can just call a slot, but queue it up so that it gets called in the right thread?

Something like skypeThread->invokeSlot(startSkypeCall("someguy")) instead of defining, connecting, and emitting a signal for each function I want to call.

ktk
19th March 2010, 21:43
QMetaObject::invokeMethod(skypeThread, "startSkypeCall", Qt::QueuedConnection, Q_ARG(QString, "someguy")) might work

pherthyl
19th March 2010, 22:13
Thanks! it works.

For completeness, in pyQt it works like this:
QMetaObject.invokeMethod(skypeThread, "startSkypeCall", Qt.QueuedConnection, QtCore.Q_ARG("QString", "someguy"))

But you have to be sure to mark your slot as a pyQtSlot. Otherwise it won't work, with the very helpful error: RuntimeError: QMetaObject.invokeMethod() call failed