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. Passing objects from C++ to QML
Forum Updated to NodeBB v4.3 + New Features

Passing objects from C++ to QML

Scheduled Pinned Locked Moved QML and Qt Quick
qmlc++compile-errors
4 Posts 3 Posters 7.1k Views 3 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.
  • M Offline
    M Offline
    MTK358
    wrote on 9 Aug 2015, 00:57 last edited by
    #1

    I am having trouble understanding how objects in QML work. I see that it's possible to register a QObject subclass so that QML can make instances of it, but is it possible to make an instance of a QObject in C++ and pass it to QML? If so, how can signal handlers be connected to it, and how is the memory managed?

    This is the example I tried, but it fails with this error message:

    QMetaObject::invokeMethod: No such method
    QQuickWindowQmlImpl_QML_0::myCallback(QObject*)
    Candidates are:
        myCallback(QVariant)
    

    This is the C++ code:

    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    QObject *rootObj = engine.rootObjects().at(0);
    QObject *myObj = new QObject();
    myObj->setProperty("myStringProperty", "asdfasdf");
    QMetaObject::invokeMethod(rootObj, "myCallback", Q_ARG(QObject*, myObj));
    

    And the QML code:

    import QtQuick 2.4
    import QtQuick.Window 2.2
    Window {
        visible: true
        Rectangle {
            id: root
            anchors.fill: parent
            color: "gray"
            Text {
                id: label
                anchors.centerIn: parent
                text: "(waiting for callback)"
            }
        }
        function myCallback(obj) {
            label.text = obj.myStringProperty
        }
    }
    

    When I try replacing QObject* with QVariant in the Q_ARG, that causes a C++ compiler error.

    If this is not possible, then how else can a QML user interface be made aware of dynamically added and removed entities in the C++ code, each with some properties and events associated with them, which need a representation in the UI?

    J 1 Reply Last reply 9 Aug 2015, 02:38
    0
    • M MTK358
      9 Aug 2015, 00:57

      I am having trouble understanding how objects in QML work. I see that it's possible to register a QObject subclass so that QML can make instances of it, but is it possible to make an instance of a QObject in C++ and pass it to QML? If so, how can signal handlers be connected to it, and how is the memory managed?

      This is the example I tried, but it fails with this error message:

      QMetaObject::invokeMethod: No such method
      QQuickWindowQmlImpl_QML_0::myCallback(QObject*)
      Candidates are:
          myCallback(QVariant)
      

      This is the C++ code:

      QQmlApplicationEngine engine;
      engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
      QObject *rootObj = engine.rootObjects().at(0);
      QObject *myObj = new QObject();
      myObj->setProperty("myStringProperty", "asdfasdf");
      QMetaObject::invokeMethod(rootObj, "myCallback", Q_ARG(QObject*, myObj));
      

      And the QML code:

      import QtQuick 2.4
      import QtQuick.Window 2.2
      Window {
          visible: true
          Rectangle {
              id: root
              anchors.fill: parent
              color: "gray"
              Text {
                  id: label
                  anchors.centerIn: parent
                  text: "(waiting for callback)"
              }
          }
          function myCallback(obj) {
              label.text = obj.myStringProperty
          }
      }
      

      When I try replacing QObject* with QVariant in the Q_ARG, that causes a C++ compiler error.

      If this is not possible, then how else can a QML user interface be made aware of dynamically added and removed entities in the C++ code, each with some properties and events associated with them, which need a representation in the UI?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 9 Aug 2015, 02:38 last edited by
      #2

      Hi @MTK358,

      is it possible to make an instance of a QObject in C++ and pass it to QML? If so, how can signal handlers be connected to it

      Yes. Set your C++ QObject as a Context Property: http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

      That link also shows you how to make connections.

      and how is the memory managed?

      Your C++ code remains responsible for deleting that QObject: http://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      M 1 Reply Last reply 11 Aug 2015, 01:10
      4
      • J JKSH
        9 Aug 2015, 02:38

        Hi @MTK358,

        is it possible to make an instance of a QObject in C++ and pass it to QML? If so, how can signal handlers be connected to it

        Yes. Set your C++ QObject as a Context Property: http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

        That link also shows you how to make connections.

        and how is the memory managed?

        Your C++ code remains responsible for deleting that QObject: http://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

        M Offline
        M Offline
        MTK358
        wrote on 11 Aug 2015, 01:10 last edited by
        #3

        @JKSH said:

        Yes. Set your C++ QObject as a Context Property: http://doc.qt.io/qt-5/qtqml-cppintegration-contextproperties.html

        That link also shows you how to make connections.

        Thank you, that's what I was looking for.

        Your C++ code remains responsible for deleting that QObject: http://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

        What if QML connects to a signal of a context property object, that signal has a QObject* as an argument, and the JS handler code saves it in some item's property?

        Will deleting it cause a segfault in the QML interpreter? Is there a way to know that it's not referenced any more?

        1 Reply Last reply
        1
        • G Offline
          G Offline
          GTDev
          wrote on 30 Jan 2018, 18:11 last edited by
          #4

          There are several ways how to communicate between C++ and QML. The most important part is to set up your class with signals and slots to be used in QML.

          A simple guide with an example project can be found here:
          https://v-play.net/cross-platform-development/how-to-expose-a-qt-cpp-class-with-signals-and-slots-to-qml#how-to-communicate-between-cpp-and-qml

          Senior Developer at Felgo - https://felgo.com/qt

          Develop mobile Apps for iOS & Android with Qt
          Felgo is an official Qt Technology Partner

          1 Reply Last reply
          0

          • Login

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