PDA

View Full Version : Q_PROPERTY and deeply nested custom types



Sammy D'Souza
15th May 2010, 09:23
Hi
This is my first post to a Qt forum, and believe me, I'm doing this after some rather serious googling and coding. So, please be gentle, here goes:

Do you have any non-trivial example of using Q_PROPERTY macros for deeply nested classes. All I can seem to bump into are examples of nested enums or, a one level class that itself is a struct, not a QObject.

Given classes say, A, B, C, D - all QObjects, all Q_DECLARE_METATYPE declared and registered, all happy with assignment operators, copy constructors and the usual C++ goodness.

I need (and can't seem to figure out or find a clean example) to do the following.
class C has a read/write Q_PROPERTY of type B (I tried B*, that didn't work either)
class B has a read/write Q_PROPERTY of type A
class D has a read/write of type list of C

Any help would be deeply appreciated. This is on Qt 4.6 Jan 2010 cut (though same results on 4.7 beta 1). Note, I did find QML examples of the same, just can't use Qml at this point.

Thanks
Sammy

wysota
15th May 2010, 12:26
Let's make one thing clear first. QVariant (which is what Qt property system uses) requires its "members" to be copiable (or to say properly to implement a public copy constructor). QObject doesn't fill this requirement, it's "identity-based" and we operate on it using pointers. So if your A, B, C, D classes inherit QObject somewhere along the line, they can't be put into QVariant (as objects). You can only put pointers to them into QVariant. Trying to go around it will sooner or later result in failure (even if you provide a copy constructor for your object, you still won't be able to copy the QObject sitting below your class).

So you need to answer two questions first:

do I need my objects to inherit QObject?
do I need my objects to be values of properties (aka "what benefit do I have from my objects being values of properties")?