Dynamic properties
-
Hi
I've read about the dynamic properties in Qt
I know there is something in .NET for what is called the Reflection namespace, using this name space we were able to probe information about class in the runtime.My question is: if you have experience in Qt where did you used the dynamic property feature in practice?
Thanks
-
You can use that to store additional info for an object. I rarely used it, but, e.g. If you want to connect input objects (LineEdit, ComboBox, etc) with process values you could store the process value name, address in a dynamic property of the input widget, so you don't have to create some custom mapping in source code.
-
It can be a handy mechanism to attach information to an object that does not provide its own API for that. I have used it, but it is not something to rely on too much IMHO. Often, it is a quick-and-dirty way to add properties to an object that could (and perhaps should?) be added to the object using subclassing instead. However, that is not always trivial or even desireable, and in such cases this can be a handy escape route.
The main downsides are, IMO:
that you cannot see that such a property is/can be attached at runtime because it does not show up in the class definition (other than the generic method in QObject itself, ofcourse), and that makes it harder to understand the source;
that it is a non-typesave way to work (I don't considder QVariant nicely type save).
-
Thanks Gerlf and Andre for information
The only logical and frequent use has just come up in my mind: it's like a tag associated with each object, actually I remember there was a tag in .NET
Anyway thanks for your replies.
-
[quote author="Andre" date="1320226863"]Often, it is a quick-and-dirty way to add properties to an object that could (and perhaps should?) be added to the object using subclassing instead.[/quote]
There are situations where you cannot use subclasses, for example QApplication and QDeclarativeView in conjunction with the Harmattan Booster / MDeclarativeCache. In such situations dynamic properties are actually quite handy to "attach" some data to objects - which becomes even globally accessible when attached to QApplication.