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. Call a qml function from C++.
Forum Updated to NodeBB v4.3 + New Features

Call a qml function from C++.

Scheduled Pinned Locked Moved QML and Qt Quick
8 Posts 4 Posters 3.6k 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.
  • mrdebugM Offline
    mrdebugM Offline
    mrdebug
    wrote on last edited by
    #1

    I normally use this line of code to invoke a qml function from c++ and all works well.
    @QMetaObject::invokeMethod((QObject*)(this->rootObjects().first()), "CallOk");@
    But my situation in a bit different. From the main qml file, by clicking on a button, I load a second qml file.
    How can I call its functions from c++?
    This line of code does not work.
    #QMetaObject::invokeMethod((QObject*)(this->rootObjects().first()), "CallOk");#
    I have tried
    @this->rootObjects().first()->findChild<QObject*>("aaa");@
    using the name of the object but does not work.
    Can you suggest me the right way?

    Need programmers to hire?
    www.labcsp.com
    www.denisgottardello.it
    GMT+1
    Skype: mrdebug

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Babalas
      wrote on last edited by
      #2

      Use slots or Q_INVOKABLE.
      c++
      @class Blah {
      ....
      public slots:
      void iveBeenClicked()
      };@

      Pass class to qml
      @
      Blah foo;
      view->rootContext()->setContextProperty("myblah", &blah);
      @

      Qml
      @
      Button {
      onClicked: myblah.iveBeenClicked()
      }

      Button {
      id: lateButton
      }

      Component.onCompleted: lateButton.clicked.connect(myblah.iveBeenClicked())
      @

      1 Reply Last reply
      0
      • mrdebugM Offline
        mrdebugM Offline
        mrdebug
        wrote on last edited by
        #3

        Sorry but it isn't my situation.
        I need to call a qml function from c++.
        This line of code
        @QMetaObject::invokeMethod((QObject*)(this->rootObjects().first()), "CallOk");@
        works only with a function in the main qml file.
        If you load a second qml file, the functions in it are not accesible from C++.

        Need programmers to hire?
        www.labcsp.com
        www.denisgottardello.it
        GMT+1
        Skype: mrdebug

        1 Reply Last reply
        0
        • G Offline
          G Offline
          Gennon
          wrote on last edited by
          #4

          Why not emit a signal from C++ that the QML file catches and then runs your function?

          /Gen

          1 Reply Last reply
          0
          • mrdebugM Offline
            mrdebugM Offline
            mrdebug
            wrote on last edited by
            #5

            It's a nice idea but I haven't find an example on how to connect a qml function to a c++ signal.
            Connect function seems to not be available in qml.

            Need programmers to hire?
            www.labcsp.com
            www.denisgottardello.it
            GMT+1
            Skype: mrdebug

            1 Reply Last reply
            0
            • G Offline
              G Offline
              Gennon
              wrote on last edited by
              #6

              You just make a C++ object that emits a signal the normal way, then make use of "Connections":http://qt-project.org/doc/qt-5/qml-qtqml-connections.html in QML. You need to have the "class available to QML":http://qt-project.org/doc/qt-5/qtqml-cppintegration-contextproperties.html.

              So if you have a signal runFunction in your C++ class you can then just connect to it in your QML file like this:

              @
              Connections {
              target: myCppClass
              onRunFunction: myQMLFuntion();
              }
              @

              Hope it helps!

              /Gen

              1 Reply Last reply
              0
              • p3c0P Offline
                p3c0P Offline
                p3c0
                Moderators
                wrote on last edited by
                #7

                [quote author="mrdebug" date="1405861088"]

                I normally use this line of code to invoke a qml function from c++ and all works well.
                @QMetaObject::invokeMethod((QObject*)(this->rootObjects().first()), "CallOk");@
                But my situation in a bit different. From the main qml file, by clicking on a button, I load a second qml file.
                How can I call its functions from c++?
                This line of code does not work.
                #QMetaObject::invokeMethod((QObject*)(this->rootObjects().first()), "CallOk");#
                I have tried
                @this->rootObjects().first()->findChild<QObject*>("aaa");@
                using the name of the object but does not work.
                Can you suggest me the right way?[/quote]

                Try this,
                @
                QMetaObject::invokeMethod((QObject*)(engine.rootObjects().at(0)->findChild<QQuickItem*>("myObject")), "callMyMethod");
                @

                Provided you have given an objectName to the Item (here myObject)

                157

                1 Reply Last reply
                0
                • mrdebugM Offline
                  mrdebugM Offline
                  mrdebug
                  wrote on last edited by
                  #8

                  @
                  Connections {
                  target: myCppClass
                  onRunFunction: myQMLFuntion();
                  }
                  @

                  Works perfectly!

                  Need programmers to hire?
                  www.labcsp.com
                  www.denisgottardello.it
                  GMT+1
                  Skype: mrdebug

                  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