Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Emitting signal from different thread causes QML to crash
Qt 6.11 is out! See what's new in the release blog

Emitting signal from different thread causes QML to crash

Scheduled Pinned Locked Moved QML and Qt Quick
2 Posts 2 Posters 2.6k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    pholz
    wrote on last edited by
    #1

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

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kniebou
      wrote on last edited by
      #2

      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@

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved