PDA

View Full Version : Q_PROPERTIES when to use it?



radek.z
10th February 2009, 09:27
Hi,
can somebody tell me when is it good to use this macro for myClass read/write properties? I couldn't really release even I red doc.

Radek

talk2amulya
10th February 2009, 09:39
Hi,

This property system is generally used in conjunction with the meta-object system that QT uses. It primarily is used to make function calls faster(used for setting and getting properties). Plus even if u dont know anything about the class at the compile time, u can access their properties. Also, if you think about it, it provides two seperate ways to access property, accessing via meta-object system being the faster one.

Amulya

radek.z
10th February 2009, 11:12
Hi,
OK, so generally, as I understand it might be good to use it in any of my class, which inherits from QObject and which has get and set functions for class properties.
Is that right?
radek

talk2amulya
10th February 2009, 11:17
generally its always useful to employ it in ur code cuz it does provide better way to access properties.For example, Lets consider a window class. A window may have properties like width, height, current coordinates etc. In these cases, using property system is very useful as it will provide u with multiple ways and more efficient ways to access specific properties cuz these will be saved in the meta-object system of QT. This is one of the major points in favor of Qt cuz it provides a string based system that also helps in RTTI and many functionalities that compilers also use internally.

radek.z
10th February 2009, 11:28
great, thanks for replaying. I have another question, how should i do it in case I have 1 get and 2 set func. When I declare Q_PROPERTY the READ is compulsory, can I use it twice in two Q_PROPERTY or I can use two different WRITE in one Q_PROPERTY.
radek

talk2amulya
10th February 2009, 11:52
well, i believe its easily possible using method overriding. The WRITE functions can only take three type of arguments, either the type of property, or a pointer or reference to it..so considering the following:

Q_PROPERTY( QString label READ label WRITE setLabel )

setLabel can have a QString* or QString& or QString as parameter, thus making essentially two different functions.

radek.z
10th February 2009, 12:26
Thanks a lot for your replies. I think I got it so far.
Radek