Search code examples
qtqmlsignals-slotsqtquick2

Signal from QML to C++ slot, cannot find the QML signal


I got a signal in QML and I want to connect to a slot defined in C++. However my code is failing and I'm receiving the error:

QObject::connect: No such signal QDeclarativeContext::sent() in ../qt_cpp/mainwindow.cpp:66

Here is a c++ code snippet:

message_reading test;
QDeclarativeView tempview;
tempview.setSource(QUrl("qrc:/qml/media_screen.qml"));
QObject *item = tempview.rootContext();
QObject::connect(item, SIGNAL(sent()),
&test, SLOT(readMediaJSONDatabase(QString&)));

And here is a QML code snippet:

Image {
    id: bluetooth
    source: "images_mediahub/footer_icons/bluetooth_normal.png"
    signal sent(string msg)
    MouseArea {
        anchors.fill:  parent
        onClicked: {
            bluetooth.sent(Test.medialibrarydb.toString())
            info_load.source="./bluetooth.qml"
        }
    }
}

Solution

  • The SIGNAL macro call in the connect line must inform the parameter explicitly, with SIGNAL(sent(QString)).

    In addition, the signal is being emitted by the created object, but code snippet you provided is trying to connect it in the context object instead. You'll need something along the lines of:

    QObject *tempitem = tempview.rootObject();
    

    There's a complete example covering that in the documentation.