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. Newbie - shared object between C++ and QML
QtWS25 Last Chance

Newbie - shared object between C++ and QML

Scheduled Pinned Locked Moved Solved QML and Qt Quick
6 Posts 4 Posters 548 Views
  • 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.
  • SteMMoS Offline
    SteMMoS Offline
    SteMMo
    wrote on last edited by
    #1

    Hi all,
    I'm trying to share a C++ object with QML code.
    I defined a IoBoard class and instantiated.
    Then shared it with QML cause I need to recall some functions.
    In my debug prints I see that the engine created two objects !

    IoBoard ioboard;
    ioboard.setType((IoBoard::CONNECTION_SERIAL));
    
    qmlRegisterType<IoBoard>("com.my.locker", 1, 0, "IoBoard");
    

    How can i have a single object?

    Thanks!

    1 Reply Last reply
    0
    • KH-219DesignK Offline
      KH-219DesignK Offline
      KH-219Design
      wrote on last edited by
      #4

      Do you have a call anywhere in your application to "QQmlContext::setContextProperty"?

      https://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

      If not, then that could be the one piece you are missing.

      I have a template project (that is built and tested regularly in GitHub CI to prove its viability) that contains an end-to-end working example:

      • Here are the calls to setContextProperty: https://github.com/219-design/qt-qml-project-template-with-ci/search?q=setcontextproperty&unscoped_q=setcontextproperty
      • Here is where the QML code then uses customLoggingCategories (one of the C++ objects exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/c61bbd4fbfb4aeb4f9c96b5baedeb935e3064ae9/src/lib_app/qml/LogTags.qml
      • Here is where the QML code uses resourceHelper (another object exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/79e71e40be0874dd9fcc229b9685a4371697d3cf/src/lib_app/qml/main.qml

      You can also download a source distribution of the Qt5 sources and look at their examples, which are often excellent.

      You might not be particularly interested in Video Effects, but this official Qt example uses setContextProperty and exposes several C++ objects to QML: https://doc.qt.io/qt-5/qtmultimedia-multimedia-video-qmlvideofx-example.html

      So does this one: https://doc.qt.io/qt-5.9/qtquickcontrols2-gallery-example.html

      Code:

      • https://github.com/qt/qtmultimedia/tree/dev/examples/multimedia/video/qmlvideofx
      • https://github.com/qt/qtquickcontrols2/tree/dev/examples/quickcontrols2/gallery

      www.219design.com
      Software | Electrical | Mechanical | Product Design

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

        Hi,

        How are you using it in your QML code ?

        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
        0
        • SteMMoS Offline
          SteMMoS Offline
          SteMMo
          wrote on last edited by
          #3

          In this way:

          IoBoard {
              id: ioBoard
          }
          
          {...
            onClicked:  ioBoard.openLock(2)
          ...}
          
          SGaistS 1 Reply Last reply
          0
          • KH-219DesignK Offline
            KH-219DesignK Offline
            KH-219Design
            wrote on last edited by
            #4

            Do you have a call anywhere in your application to "QQmlContext::setContextProperty"?

            https://doc.qt.io/qt-5/qqmlcontext.html#setContextProperty

            If not, then that could be the one piece you are missing.

            I have a template project (that is built and tested regularly in GitHub CI to prove its viability) that contains an end-to-end working example:

            • Here are the calls to setContextProperty: https://github.com/219-design/qt-qml-project-template-with-ci/search?q=setcontextproperty&unscoped_q=setcontextproperty
            • Here is where the QML code then uses customLoggingCategories (one of the C++ objects exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/c61bbd4fbfb4aeb4f9c96b5baedeb935e3064ae9/src/lib_app/qml/LogTags.qml
            • Here is where the QML code uses resourceHelper (another object exported to QML): https://github.com/219-design/qt-qml-project-template-with-ci/blob/79e71e40be0874dd9fcc229b9685a4371697d3cf/src/lib_app/qml/main.qml

            You can also download a source distribution of the Qt5 sources and look at their examples, which are often excellent.

            You might not be particularly interested in Video Effects, but this official Qt example uses setContextProperty and exposes several C++ objects to QML: https://doc.qt.io/qt-5/qtmultimedia-multimedia-video-qmlvideofx-example.html

            So does this one: https://doc.qt.io/qt-5.9/qtquickcontrols2-gallery-example.html

            Code:

            • https://github.com/qt/qtmultimedia/tree/dev/examples/multimedia/video/qmlvideofx
            • https://github.com/qt/qtquickcontrols2/tree/dev/examples/quickcontrols2/gallery

            www.219design.com
            Software | Electrical | Mechanical | Product Design

            1 Reply Last reply
            0
            • SteMMoS SteMMo

              In this way:

              IoBoard {
                  id: ioBoard
              }
              
              {...
                onClicked:  ioBoard.openLock(2)
              ...}
              
              SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #5

              @SteMMo said in Newbie - shared object between C++ and QML:

              In this way:

              IoBoard {
                  id: ioBoard
              }
              
              {...
                onClicked:  ioBoard.openLock(2)
              ...}
              

              And here you are: you instanciate it once in your main function and you create another one here.

              Either remove the one from you main or remove this one and use a context property as suggested by @KH-219Design.

              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
              • S Offline
                S Offline
                Shazter
                wrote on last edited by
                #6

                Is very well described here: https://qml.guide/singletons/ and here https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonType. Is good to know which Qt Version you are using, because registration has been changed in Qt 5.15.0. Unfortunately I cannot find the drawing in the Qt Documentation, where was recommended which solution you should use qmlRegisterType / setcontextProperrty etc. in some use cases.

                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