Invoke QML method from C++



  • Hi! I want to invoke a method in MyMap from my class MyBacklogg, how do I do this when I don't have the object parameters in that class?

    I'm going to receive a string, a QByteArray or a QDataStream in MyBacklogg, depending on which one that works the best, and I want to pass these along to my GUI.

    main.cpp

    int main(int argc, char *argv[])
    {
        QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication app(argc, argv);
    
        QQmlApplicationEngine engine;
        engine.load(QUrl(QLatin1String("qrc:/qml/main.qml")));
        if (engine.rootObjects().isEmpty())
            return -1;
    
        QObject *item = engine.rootObjects().first();
        MyTcpSocket s;
        s.init();
        QObject::connect(item, SIGNAL(sendSignal()), &s, SLOT(doSlot()));
    
        return app.exec();
    }
    

    main.qml

    ApplicationWindow {
        id: appWindow
        visible: true
        height: 600
        width: 800
        title: qsTr("MyApp")
        signal sendSignal()
    
        RowLayout{
            visible: true
            anchors.fill: parent
    
            MyMap {
                id: mapview
                function myFunc() {
                    //function called from MyBacklogg
                }
                function myOtherFunc() {
                    sendSignal()
                }
            }
        }
    }
    

    MyBacklogg.cpp

    #include "mybacklogg.h"
    
    MyBacklogg::MyBacklogg(QObject *parent) :
        QObject(parent)
    {
    }
    
    void MyBacklogg::init()
    {
        //initialize
    }
    
    void MyBacklogg::doSlot()
    {
        //function call from MyMap
    }
    
    void MyBacklogg::callMethod()
    {
        ??????????????????????????????
        QMetaObject::invokeMethod(object, "myFunc",
            Q_RETURN_ARG(QVariant, returnedValue),
            Q_ARG(QVariant, msg));
        ??????????????????????????????
    }
    


  • Hi! My standard approach is described in this thread.


Log in to reply
 

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