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. Send a signal to a QML form embedded in a QQuickWidget
Forum Updated to NodeBB v4.3 + New Features

Send a signal to a QML form embedded in a QQuickWidget

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 906 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.
  • E Offline
    E Offline
    EqiupmentConnected
    wrote on last edited by
    #1

    I have a QML form that I display inside of a QQuickWidget in a C++ class. I want to be able to send a signal to the QML form from the class that the QQuickWidget is created in.

    eg.

    Class MainWindow
    {
    ...
    qmlwidget = new QQuickWidget()
    qmlwidget->setSource(QUrl::fromLocalFile(filepath)
    }

    Is there a way to reference the QML form so I can connect a signal to it? If not a signal, is there a way to trigger the functions of the QML form from the parent C++ class?

    JoeCFDJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Did you already take a look at the QtQml C++ Integration chapter in Qt's documentation ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • E EqiupmentConnected

        I have a QML form that I display inside of a QQuickWidget in a C++ class. I want to be able to send a signal to the QML form from the class that the QQuickWidget is created in.

        eg.

        Class MainWindow
        {
        ...
        qmlwidget = new QQuickWidget()
        qmlwidget->setSource(QUrl::fromLocalFile(filepath)
        }

        Is there a way to reference the QML form so I can connect a signal to it? If not a signal, is there a way to trigger the functions of the QML form from the parent C++ class?

        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #3

        @EqiupmentConnected get QQuickItem * quick_item = QQuickWidget->rootObject();
        if you want to change a value defined in qml, use the following:
        https://doc.qt.io/qt-5/qobject.html#setProperty

        GrecKoG 1 Reply Last reply
        1
        • JoeCFDJ JoeCFD

          @EqiupmentConnected get QQuickItem * quick_item = QQuickWidget->rootObject();
          if you want to change a value defined in qml, use the following:
          https://doc.qt.io/qt-5/qobject.html#setProperty

          GrecKoG Offline
          GrecKoG Offline
          GrecKo
          Qt Champions 2018
          wrote on last edited by
          #4

          @JoeCFD no don't do that, that's bad practice.
          https://doc.qt.io/qt-6/qtquick-bestpractices.html#separate-ui-from-business-logic
          https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
          https://youtu.be/vzs5VPTf4QQ?t=23m20s

          Instead expose an object as a Singleton to QML and use the Connections type to handle its signal.

          JoeCFDJ 1 Reply Last reply
          3
          • GrecKoG GrecKo

            @JoeCFD no don't do that, that's bad practice.
            https://doc.qt.io/qt-6/qtquick-bestpractices.html#separate-ui-from-business-logic
            https://doc.qt.io/qt-6/qtqml-cppintegration-overview.html#interacting-with-qml-objects-from-c
            https://youtu.be/vzs5VPTf4QQ?t=23m20s

            Instead expose an object as a Singleton to QML and use the Connections type to handle its signal.

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #5

            @GrecKo Good to know. Thanks.

            1 Reply Last reply
            0
            • E Offline
              E Offline
              EqiupmentConnected
              wrote on last edited by
              #6

              Got it to work using:

              QObject *rootObject = qmlwidget->rootObject()
              QMetaObject::invokeMethod(rootObject, "functionName")

              https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

              Thanks for the help.

              GrecKoG 1 Reply Last reply
              0
              • E EqiupmentConnected has marked this topic as solved on
              • E EqiupmentConnected

                Got it to work using:

                QObject *rootObject = qmlwidget->rootObject()
                QMetaObject::invokeMethod(rootObject, "functionName")

                https://doc.qt.io/qt-5/qtqml-cppintegration-interactqmlfromcpp.html#invoking-qml-methods

                Thanks for the help.

                GrecKoG Offline
                GrecKoG Offline
                GrecKo
                Qt Champions 2018
                wrote on last edited by
                #7

                😬😬😬😬😬

                JoeCFDJ 1 Reply Last reply
                1
                • GrecKoG GrecKo

                  😬😬😬😬😬

                  JoeCFDJ Offline
                  JoeCFDJ Offline
                  JoeCFD
                  wrote on last edited by JoeCFD
                  #8

                  @GrecKo I guess I understood what your idea is. It is similar like creating a model for listview. Change the data in the model to update QML code instead of settting the properties into QML code directly.

                  An example:
                  https://scythe-studio.com/en/blog/how-to-integrate-c-and-qml-registering-c-class-as-singleton-to-qml

                  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