How do I create a handler for a signal from my c++ object?



  • My QtQuick application is initialized as shown below from c++ main(). I have a signal from myObject, that I wish to handle in the QML pages but I can't figure out how. The last part of this guide (http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html) touches on it, but doesn't work for me. I think it may be because the object is created from main() and not from within the QML.

        MyObjectType myObject;
        QQuickView *view = new QQuickView;
        view->rootContext()->setContextProperty("myObject", &myObject);
        view->setSource(QUrl("qrc:/main.qml"));
        view->setResizeMode(QQuickView::SizeRootObjectToView);
        view->show();
    

    Here is one thing I tried within my main QML item. I tried a bunch of other things too, but now I'm stumped. This gives me the warning 'Cannot assign to non-existent property "ghoztConnecting"' on the second line.

        signal signalToHandle()
        signalToHandle: myObject.onMySignal()
        onSignalToHandle: {
        pageLoader.source="home.qml";
        }
    

    I've also tried simply this, but it seems like it only recognizes the reference to myObject within the javascript code regions.

    myObject.onMySignal: {
        pageLoader.source="home.qml";
        }
    

    Any suggestions?


  • Moderators

    @kgregory
    you need to go this way via the Connections element in QML:

    Connections {
        target: myObject
        onMySignal: {
           pageLoader.source="home.qml";
        }
    }
    

Log in to reply
 

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