Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Invoke QML method from C++
Forum Updated to NodeBB v4.3 + New Features

Invoke QML method from C++

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 738 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    RaZp29
    wrote on 30 Nov 2017, 12:29 last edited by RaZp29
    #1

    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));
        ??????????????????????????????
    }
    
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 30 Nov 2017, 12:41 last edited by
      #2

      Hi! My standard approach is described in this thread.

      1 Reply Last reply
      1

      1/2

      30 Nov 2017, 12:29

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved