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 : pass C++ instances to QML
Forum Updated to NodeBB v4.3 + New Features

Newbie : pass C++ instances to QML

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

    Hello,

    I would like to know if it is possible to instantiate an object in C++ (which exposes properties with Q_PROPERTY) and to use this object instantiated in C++ in a QML context.

    I know that to use a C++ object in QML, I have to use qmlRegisterType but I don't want to do instantiation in QML.

    Thanks for your help

    sc

    ODБOïO J.HilkJ 2 Replies Last reply
    0
    • S sebastienc

      Hello,

      I would like to know if it is possible to instantiate an object in C++ (which exposes properties with Q_PROPERTY) and to use this object instantiated in C++ in a QML context.

      I know that to use a C++ object in QML, I have to use qmlRegisterType but I don't want to do instantiation in QML.

      Thanks for your help

      sc

      ODБOïO Offline
      ODБOïO Offline
      ODБOï
      wrote on last edited by
      #2

      @sebastienc hi
      here is an overview of QML / c++ integration methods
      looks like you need this one

      1 Reply Last reply
      3
      • S sebastienc

        Hello,

        I would like to know if it is possible to instantiate an object in C++ (which exposes properties with Q_PROPERTY) and to use this object instantiated in C++ in a QML context.

        I know that to use a C++ object in QML, I have to use qmlRegisterType but I don't want to do instantiation in QML.

        Thanks for your help

        sc

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @sebastienc sure
        in your particular case:
        https://doc.qt.io/qt-5/qqmlengine.html#qmlRegisterSingletonInstance


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        1 Reply Last reply
        3
        • S Offline
          S Offline
          sebastienc
          wrote on last edited by
          #4

          Thank you, it seems to suit my needs.

          Is there an equivalent with qt 5.11 as this method was introduced in qt 5.14 ?

          B 1 Reply Last reply
          0
          • S sebastienc

            Thank you, it seems to suit my needs.

            Is there an equivalent with qt 5.11 as this method was introduced in qt 5.14 ?

            B Offline
            B Offline
            Bob64
            wrote on last edited by
            #5

            @sebastienc For older Qt, I think this is what you need:

            qmlEngine->rootContext()->setContextProperty("myobj", myCppObjInstance);

            Your object will be accessible via the global myobj property (or whatever you want to call it) in your QML.

            Obviously, your C++ object has to be a QObject and have QML-compatible members exposed on it (e.g. Q_INVOKABLE methods and/or Q_PROPERTY members) to be useful.

            S 1 Reply Last reply
            2
            • B Bob64

              @sebastienc For older Qt, I think this is what you need:

              qmlEngine->rootContext()->setContextProperty("myobj", myCppObjInstance);

              Your object will be accessible via the global myobj property (or whatever you want to call it) in your QML.

              Obviously, your C++ object has to be a QObject and have QML-compatible members exposed on it (e.g. Q_INVOKABLE methods and/or Q_PROPERTY members) to be useful.

              S Offline
              S Offline
              sebastienc
              wrote on last edited by
              #6

              @Bob64 Perfect !

              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