Q_PROPERTY[SOLVED]
-
Hello!
I'm wondering, to be honest, why in Qt is such thing as class properties? I understand, why it is in .Net: it's a convenient way to represent setter and getter for the private fields in your class and during runtime get and set methods will be generated for you. But in Qt you still have to implement them on your own. OK, you may say me, that by properties you can made the variable read-only. Isn't more convenient just forget about setVariable() -method and implement only const type getVariable()const{return var;} -method?
So what I want to ask you about is, what's the point of properties in Qt?
Thank you beforehand for answers.
p.s. It's only my curiosity and the way to learn about things.
-
Q_PROPERTY is to be seen apart from getter and setter functions. It is just a macro which enables Qt specific options/operations on a class. Only with Q_PROPERTY you can work with a class via QMetaObject and other things. (in other words you expose function to Qt, which whom Qt can work)
-
There is no such thing as a property in C++, and there is no such thing as setters and getters either.
But sometimes you are required to query and modify the properties of an object at runtime, two examples are the UI Designer and QML. The latter one takes advantage of another important property feature, change notifications.
See "The Property System":http://qt-project.org/doc/qt-4.8/properties.html.
-
Thank you, guys, for your answers.
Yes, I know, that in C++ there are no such things as properties. And about NOTIFY -part, you still have to write something like that: emit valueChanged(type).As I understood, it's only for the meta-object information and making some "black magic" with objects during run-time, right?
-
The Qt property system does two things:
It semantically groups a set of methods to create a property with a getter, and optionaly a setter, a changed signal and a reset method. So, it groups them together for you as a programmer and the users of the API's you build.
It enables the Qt Meta Object system to generate code to allow introspection of QObjects, to store some meta-information on these properties ("designable", "stored", "user"), and to provide a single way of accessing all these through the property and setProperty methods. All that can be very useful in some contexts.
-
Thank you for your answer. It looks like, that in some cases it's very useful thing to consider. How it(using Q_PROPERTY -macro) will affect on performance(compilation and executing)?
-
As code is generated for it, there will be a very small compilation time penalty. It should not have any effect on execution speed.
-
Thank you very much.
I guess, now case is closed=)