Specify Qt::QueuedConnection for connections made via the Connections QML type



  • I have an application where signals are sent between QML and a worker running in a separate QThread. When connections are made in C++ via QObject::connect, everything works as expected. I have not specified a ConnectionType; presumably the default of AutoConnection does the right thing and uses QueuedConnection since the receiver is in a different thread.

    My code could get cleaned up vastly if I could just connect via the Connections QML type as demonstrated here: https://www.wisol.ch/w/articles/2014-12-15-qt-signal-slots-qml-cpp/. I had hoped that such connections would also default to using AutoConnection, but it appears that that is not so. I'm seeing warnings like QObject::killTimer: Timers cannot be stopped from another thread which suggest that a DirectConnection is being made.

    Is there any way to specify QueuedConnection in a Connections object?



  • @vikramg
    yes of course, the connection typ is always the 5th parameter. Most of the time you don't write it down, as it has a preset of Qt::AutoConnection

    connect(qmlObj, SIGNAL(someSignal(QVariant)), cppObj, SLOT(someSlot(QVariant)),Qt::QueuedConnection );
    

    should work just fine,



  • @J.Hilk
    No, I'm talking specifically about not using the QObject::connect method; instead using a Connections object in QML, as shown in the link in my original post:

    Window {
      Connections {
        target: receiver
        onSendToQml: {
        console.log("Received in QML from C++: " + count)
        }
      }
      MouseArea {
        anchors.fill: parent
        onClicked: {
          receiver.receiveFromQml(42);
        }
      }
    }
    

    In this case, no explicit connect calls are needed; they get arranged for you via the Connections object instantiation in QML. receiveFromQml() automatically invokes a slot from QML to C++ and the sendToQml() member function of the Receiver class automatically results in an onSendToQml() signal in QML.

    In this kind of implicit signal/slot connection, is there a way to specify QueuedConnection?



  • @vikramg oh, you want to go that route.
    AFAIK theres no such thing. But you could connect your slot to a singleshot timer that basically emulates a QueuedConnection:

    Connections {
        target: receiver
        onSendToQml: { QueuedTimer.start()}
      }
    
    Timer {
        id: QueuedTimer
        interval: 1
        onTriggered:{     console.log("Received in QML from C++: " + count) }
    }
    

    But mabe someone else knows more about this topic.



  • Thanks @J.Hilk
    Yes, the C++ connect calls work fine; I did want to try with a Connections object in QML because that would clean up many connect calls in C++.


Log in to reply
 

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