PDA

View Full Version : Invoke slot from a non-QThread



stephelton
14th January 2013, 09:09
I have a callback made from another (non-Q)Thread that I need to interact with the UI. I've tried having the callback start a QTimer and invoke a slot, but I get an error explaining that I can't use QTimer in a non-QThread. What other options do I have?

Added after 31 minutes:

This works:

timer->moveToThread( QApplication::instance()->thread() );

and also, when calling connect(), pass Qt::QueuedConnection, as in:

connect( timer, SIGNAL(timeout()), this, SLOT(update()), Qt::QueuedConnection );

anda_skoa
14th January 2013, 11:33
You can always invoke a slot using QMetaObject::invokeMethod() without having to be in the context of a QObject
If you do this on a GUI object from a non-GUI thread, then use Qt::QueuedConnection as the Qt::ConnectionType argument of invokeMethod()

Cheers,
_