Sending setFocus() message from another thread.
-
Hello everyone.
Sorry for noob question.Problem: setting focus to widget from non-GUI threads.
Details:
OS: Win10 Pro x64
QT: 5.9.1 x64 for msvc2015
Comp: MSVC 2015 x64
I have QGraphicsScene and QGraphicsView as central widget. Each widget on scene presents some device (like cash-box machine). Each device polled in own thread (not QT thread). When device got an error during polling i want to set focus to device's widget and show error-description. To do this i callDeviceWidget->setFocus(Qt::OtherFocusReason);
And get debug assertion:
QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. ...
Assertion raise when my DeviceWidget send focus event to QGraphicsProxyWidget (that created when you add QWidget to QGraphicsScene).
When i test release build all work fine. But i know that assertion exists :)
So main question: how to change focus-widget from different threads (with minimal code)
And if this is impossible can i ignore this debug assertion?Best regards.
-
In your thread, add and emit a signal.
Then in your view class, connect it to a slot using Qt::QueuedConnection. In that slot, set the focus to the appropriate DeviceWidget.
-
In your thread, add and emit a signal.
Then in your view class, connect it to a slot using Qt::QueuedConnection. In that slot, set the focus to the appropriate DeviceWidget.
@sierdzio thx for response. This is non QObject threads. Is it possible to signal from them?
-
@sierdzio thx for response. This is non QObject threads. Is it possible to signal from them?
@goldstar2154 said in Sending setFocus() message from another thread.:
@sierdzio thx for response. This is non QObject threads. Is it possible to signal from them?
That makes it a bit more tricky.
You can try adding the signal in your view class, connecting (remember to set the connection to Queued explicitly), and then calling it from your thread. It is not an elegant solution, and I'm not sure if it will work, but there is a chance it will :)
Signals are const, reentrant so with a queued connection you shouldn't run into concurrency issues.
-
Hm, I wonder... perhaps it could work if you used the new signal-slot syntax. Then you could send the signal from your thread even though it is not a QObject, because function pointers are being connected. Maybe. It'll probably crash horribly or not compile. Here be dragons etc. ;)
-
Finally i dont found any way to do this think and add to my code message dispather class (derived from QObject). All threads send messages into dispather and dispather can control GUI-thread throug signal/slot mechanics with Qt::QueuedConnection flag.
Cheers)P.S. new slot syntax dont help in my case.