PDA

View Full Version : about properties



jajdoo
20th July 2011, 20:55
this might be a silly question, but still..
taking the following example from the qt docs:

class MyClass : public QObject
{
Q_OBJECT
Q_PROPERTY(Priority priority READ priority WRITE setPriority NOTIFY priorityChanged)
Q_ENUMS(Priority)

public:
MyClass(QObject *parent = 0);
~MyClass();

enum Priority { High, Low, VeryHigh, VeryLow };

void setPriority(Priority priority)
{
m_priority = priority;
emit priorityChanged(priority);
}
Priority priority() const
{ return m_priority; }

signals:
void priorityChanged(Priority);

private:
Priority m_priority;
};
I'm having some trouble understanding this: if the property is not dynamic, declared as a class member and i am responsible for emitting its signals and writing the methods it uses, who am i declaring Q_PROPERTY for? what are the advantages of predefined properties over dynamic ones?

mcosta
21st July 2011, 09:16
Without Q_PROPERTY you cannot use property related functions (in example QStateMachine, Animations, ...)

Static properties are declared in the class interface so they are explicit for the class users.

jajdoo
21st July 2011, 12:57
i'm still kind of lost about this one.. a bit of a hypothetical question, but:
say i have a class that holds a 3d object's properties (location, rotation, etc..) - when would or wouldn't you declare them static properties?

high_flyer
21st July 2011, 14:24
I think you don't understand what Q_PROPERTY does.
Read here:
http://doc.qt.nokia.com/latest/qobject.html#Q_PROPERTY

jajdoo
21st July 2011, 15:09
i do understand what it does, but not completely understand its purpose..

my question became rather vague.. ill re-ask it:
if a property is dynamic, it does makes sense in my head.
if a property is not dynamic, and i need to write its READ, WRITE, NOTIFY etc.. myself, why would i declare it Q_PROPERTY? its already visible and accessible to clients - who or what require the Q_PROPERTY declaration? if i am using the class in pre-compile, i already see how to approach this class member.

i'm probably missing something fundamental here..

high_flyer
21st July 2011, 15:15
Properties behave like class data members, but they have additional features accessible through the Meta-Object System.
For example, this way you export properties to Qt designer, to name one use.
read more here:
http://doc.qt.nokia.com/latest/properties.html#qt-s-property-system