How can I listen to a C++ signal from QML?

Ross Rogers

I have what I'm calling a C++ "service" who's interface I want to expose to QML. I'm trying to use QQmlContext's setContextProperty to link the object into the QML and connect to it from a QML Connections block.

QML isn't complaining with a reference error as it did previously when I hadn't registered the service in the QML context:

qrc:/main.qml:13: ReferenceError: service is not defined

So, QML seems to find the service object now, however the QML slot javascript function is not getting invoked. I see this in Qt Creator:

Debugging starts
QML debugging is enabled. Only use this in a safe environment.
QML Debugger: Waiting for connection on port 62301...
Calling the clbk signal
Debugging has finished

There should be an In onClbk message per console.log("In onClbk"); I know that I can use QMetaObject::invokeMethod to invoke a QML object's function directly, but I am trying to have a little looser coupling through the use of signals and slots.

I would like to avoid creating a QQuickItem and instantiating the service in the QML, if at all possible.

Unfortunately, the boilerplate code is legion and this is my SSCCE.

Here is a zip file of all the project directory as created through Qt Creator 5.4.

main.cpp

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;
    auto rc = engine.rootContext();
    auto service = new Service();
    engine.rootContext()->setContextProperty(QStringLiteral("service"), service);
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

    // invoke the trigger arbitrarily
    QTimer *timer = new QTimer();
    timer->setSingleShot(true);
    QObject::connect(timer, SIGNAL(timeout()), service, SLOT(trigger_clbk()));
    timer->start(4000);
    return app.exec();
}

service.h

class Service : public QQuickItem {
    Q_OBJECT

public:
    virtual ~Service(){}
signals:
    void clbk();
public slots:
    void trigger_clbk() {
        qDebug()<<"Calling the clbk signal";
        clbk();
    }
};

main.qml

import QtQuick 2.4
import QtQuick.Window 2.2

Window {
    visible: true
    MainForm {
        anchors.fill: parent
        mouseArea.onClicked: {
            Qt.quit();
        }
        // subscribe to a signal
        Connections {
            target: service
            onClbk: function(){
                console.log("In onClbk");
            }
        }
    }
}

Main.ui.qml

import QtQuick 2.3

Rectangle {
    property alias mouseArea: mouseArea

    width: 360
    height: 360

    MouseArea {
        id: mouseArea
        anchors.fill: parent
    }

    Text {
        anchors.centerIn: parent
        text: "Hello World"
    }
}
cmannett85

You're trying to assign a JS function to the cblk signal handler, that's not going to work as the signal handler is the function that handles the signal. So the Connections block should read:

Connections {
    target: service
    onClbk: {
        console.log("In onClbk");
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

QML - connect a signal from c++ to qml

From Dev

How can I listen to one event from two template handlers?

From Dev

How to emit signal in context of specific QML Item from C++ code

From Dev

How to emit signal in context of specific QML Item from C++ code

From Dev

How can I calculate the SNR of a signal from its frequency spectrum?

From Dev

How I can send signal from MouseArea to parent object (TableView)?

From Dev

How can I retrieve data from database in QML?

From Dev

how can I create a deb package from click qml application?

From Dev

How can I pass a QML object reference into Qt C++?

From Dev

Sending a signal to a QML item from C++ (Qt5)

From Dev

Passing Q_GADGET as signal parameter from C++ to QML

From Dev

C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

From Dev

Passing Q_GADGET as signal parameter from C++ to QML

From Dev

How to bind to a signal from a delegate component within a ListView in QML

From Dev

How to bind to root context object signal from QML

From Dev

How to connect a qml child component signal to a c++ slot

From Dev

Can I use Firebase from QML?

From Dev

How to catch C++ signal in QML signal handler after type registration?

From Dev

How to catch C++ signal in QML signal handler after type registration?

From Dev

How can I listen to UDP ports in Java?

From Dev

How can I listen to session destroyed event?

From Dev

How can I listen to UDP ports in Java?

From Dev

How can I listen to a RactiveJS custom component?

From Dev

Meteor & noUiSlider: how can I listen to events?

From Dev

How can I disconnect a django signal?

From Dev

How can I pass in a $1 into a trap and signal

From Dev

How can I check if signal present in object?

From Dev

How can I check if signal present in object?

From Dev

How can I reorder the items in a QML GridLayout?

Related Related

  1. 1

    QML - connect a signal from c++ to qml

  2. 2

    How can I listen to one event from two template handlers?

  3. 3

    How to emit signal in context of specific QML Item from C++ code

  4. 4

    How to emit signal in context of specific QML Item from C++ code

  5. 5

    How can I calculate the SNR of a signal from its frequency spectrum?

  6. 6

    How I can send signal from MouseArea to parent object (TableView)?

  7. 7

    How can I retrieve data from database in QML?

  8. 8

    how can I create a deb package from click qml application?

  9. 9

    How can I pass a QML object reference into Qt C++?

  10. 10

    Sending a signal to a QML item from C++ (Qt5)

  11. 11

    Passing Q_GADGET as signal parameter from C++ to QML

  12. 12

    C++/QML: ListView is not updated on dataChanged signal from QAbstractListModel

  13. 13

    Passing Q_GADGET as signal parameter from C++ to QML

  14. 14

    How to bind to a signal from a delegate component within a ListView in QML

  15. 15

    How to bind to root context object signal from QML

  16. 16

    How to connect a qml child component signal to a c++ slot

  17. 17

    Can I use Firebase from QML?

  18. 18

    How to catch C++ signal in QML signal handler after type registration?

  19. 19

    How to catch C++ signal in QML signal handler after type registration?

  20. 20

    How can I listen to UDP ports in Java?

  21. 21

    How can I listen to session destroyed event?

  22. 22

    How can I listen to UDP ports in Java?

  23. 23

    How can I listen to a RactiveJS custom component?

  24. 24

    Meteor & noUiSlider: how can I listen to events?

  25. 25

    How can I disconnect a django signal?

  26. 26

    How can I pass in a $1 into a trap and signal

  27. 27

    How can I check if signal present in object?

  28. 28

    How can I check if signal present in object?

  29. 29

    How can I reorder the items in a QML GridLayout?

HotTag

Archive