Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Best way to export Q_PROPERTY in a wrapper?
Qt 6.11 is out! See what's new in the release blog

Best way to export Q_PROPERTY in a wrapper?

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 1.2k Views 2 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.
  • R Offline
    R Offline
    rongcuid
    wrote on last edited by
    #1

    I have a class A derived from QObject, which has some properties and may be moved to another thread. I have another class AWrapper wrapping this object, and this class is exposed to QML. What is the most succinct way to "export" properties in A to AWrapper?

    Currently I have a few options:

    1. Duplicate the members in the wrapper, and use Q_PROPERTY(member MEMBER m_member NOTIFY memberChanged), and connect the changed signal to A::setMember.
    2. Call setter/getter directly: Q_PROPERTY(member READ member WRITE setMember) where the setter/getter directly call A's setter/getter
    3. Like 2, but instead of calling setter/getter directly, make new signals to connect to A's setter/getter.

    I used 1 and 2 in my project, but I like none of these solutions. (1) duplicates data, (2) is potentially unsafe, and (3) involves a lot of boilerplate. Is there a better way to export properties in a wrapper class?

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

      Hi and welcome to devnet,

      Why do you need that wrapper ? A being a QObject you can directly use it in QML.

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

      R 1 Reply Last reply
      0
      • SGaistS SGaist

        Hi and welcome to devnet,

        Why do you need that wrapper ? A being a QObject you can directly use it in QML.

        R Offline
        R Offline
        rongcuid
        wrote on last edited by
        #3

        @SGaist I need to put A in a separate thread while being able to instantiate it in QML. I cannot find another way to do so except let AWrapper create A and move it to a separate QThread.

        I am new to Qt, though, so I may have missed something.

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

          By instantiating in QML do you mean create the objects there or pass them as property context ?

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

          R 1 Reply Last reply
          0
          • SGaistS SGaist

            By instantiating in QML do you mean create the objects there or pass them as property context ?

            R Offline
            R Offline
            rongcuid
            wrote on last edited by
            #5

            @SGaist I mean creating the objects, like:

            Item {
            A {
            id: a
            // Other properties, signals, etc
            }
            }
            

            Currently I register AWrapper with name A

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

              So you would like to achieve something similar to the QtQuick Threading example ?

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

              R 2 Replies Last reply
              0
              • SGaistS SGaist

                So you would like to achieve something similar to the QtQuick Threading example ?

                R Offline
                R Offline
                rongcuid
                wrote on last edited by
                #7

                @SGaist I want the threading like WorkerScript, but I must use a C++ class because the model of my program is written in C++. I don't see how WorkerScript helps accessing all the properties and signals of the model class.

                1 Reply Last reply
                0
                • SGaistS SGaist

                  So you would like to achieve something similar to the QtQuick Threading example ?

                  R Offline
                  R Offline
                  rongcuid
                  wrote on last edited by
                  #8

                  @SGaist I attempted to do this:

                  WorkerScript {
                    property A: A { .. }
                  }
                  

                  This doesn't work, as A is still in the main thread, blocking the GUI.

                  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