Qt World Summit: Submit your Presentation

Best way to export Q_PROPERTY in a wrapper?

  • 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?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

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

  • @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.

  • Lifetime Qt Champion

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

  • @SGaist I mean creating the objects, like:

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

    Currently I register AWrapper with name A

  • Lifetime Qt Champion

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

  • @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.

  • @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.

Log in to reply