Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Emitting signal from different thread causes QML to crash
pholz last edited by
"This bug":https://bugreports.qt-project.org/browse/QTBUG-25647 , which seems to have been solved a long time ago, seems to cause a segfault in the QML signal handler in my program built with qt 5.1.1. I googled the exact error output, which is:
Fatal error in ../3rdparty/v8/src/isolate.h, line 446
CHECK(isolate != __null) failed
And that led me to the abovementioned bug.
StateTracker * st = new StateTracker();
QQmlContext * ctx = viewer.rootContext();
QThread *sthread = new QThread();
class StateTracker : public QObject
void positionUpdateTyped(int id, QVector3D pos);
It worked fine when I was emitting the signal from the main thread, but not now that the StateTracker object is in a different thread. If I manually connect a signal from StateTracker to a QML function (as slot) via connect(), it also works - but the qt docs say that this is bad practice.
EDIT: I am able to work around it by adding a "relay" class with thread affinity to the main thread that has a signal and a slot for each signal of StateTracker, and acts as an intermediary:
void positionUpdateTypedR(int id, QVector3D pos);
void positionUpdateTyped(int id, QVector3D pos)
emit positionUpdateTypedR(id, pos);
and then I use the relay class as context property and react to onPositionUpdateTypedR in main.qml. But this adds a lot of unnecessary code for every signal.
Kniebou last edited by
I answer to that old thread because I have the exact same issue with Qt 5.2.1 and Qt 5.3 RC.
Is it possible to use Connections to connect a C++ signal from a non-main thread to a QML slot ?
The error I get is
@QObject::setParent: Cannot set parent, new parent is in a different thread@