Unsolved Best way to export Q_PROPERTY in a wrapper?
-
I have a class
A
derived fromQObject
, which has some properties and may be moved to another thread. I have another classAWrapper
wrapping this object, and this class is exposed to QML. What is the most succinct way to "export" properties inA
toAWrapper
?Currently I have a few options:
- Duplicate the members in the wrapper, and use
Q_PROPERTY(member MEMBER m_member NOTIFY memberChanged)
, and connect the changed signal toA::setMember
. - Call setter/getter directly:
Q_PROPERTY(member READ member WRITE setMember)
where the setter/getter directly callA
's setter/getter - 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?
- Duplicate the members in the wrapper, and use
-
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 letAWrapper
createA
and move it to a separateQThread
.I am new to Qt, though, so I may have missed something.
-
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 nameA
-
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 howWorkerScript
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.