Emitting signal from different thread causes QML to crash



  • "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.

    Code:

    main.cpp:
    @
    StateTracker * st = new StateTracker();
    QtQuick2ApplicationViewer viewer;
    QQmlContext * ctx = viewer.rootContext();
    ctx->setContextProperty("stateTracker", st);
    viewer.setMainQmlFile(QStringLiteral("qml/quicktest/main.qml"));
    viewer.showExpanded();
    QThread *sthread = new QThread();
    st->moveToThread(sthread);
    sthread->start();
    @

    StateTracker.h:
    @
    class StateTracker : public QObject
    {
    Q_OBJECT
    // ...
    signals:
    void positionUpdateTyped(int id, QVector3D pos);
    // ...
    };
    @

    main.qml:
    @
    Item {
    Connections {
    target: stateTracker
    onPositionUpdateTyped: {
    // doSomething
    }
    }
    }
    @

    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.

    Any ideas?

    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:

    @
    signals:
    void positionUpdateTypedR(int id, QVector3D pos);

    public slots:
    void positionUpdateTyped(int id, QVector3D pos)
    {
    emit positionUpdateTypedR(id, pos);
    }
    @

    and

    @
    QObject::connect(st, SIGNAL(positionUpdateTyped(int,QVector3D)),
    relay, SLOT(positionUpdateTyped(int,QVector3D)),
    Qt::QueuedConnection);
    @

    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.



  • Hi,

    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@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.