[Qt-interest] Q_PROPERTY confusion

Tony Rietwyk tony.rietwyk at rightsoft.com.au
Mon Jun 22 02:18:50 CEST 2009


Colin wrote:

> Hello,
> 
> I'm confused about using the Q_PROPERTY macro.  I've read the page in
> the documentation, and it gives this example:
> 
> class MyClass : public QObject
>  {
>      Q_OBJECT
>      Q_PROPERTY(Priority priority READ priority WRITE setPriority)
>      Q_ENUMS(Priority)
> 
>  public:
>      MyClass(QObject *parent = 0);
>      ~MyClass();
> 
>      enum Priority { High, Low, VeryHigh, VeryLow };
> 
>      void setPriority(Priority priority);
>      Priority priority() const;
>  };
> 
> One thing I notice is that both the member variable and the getter are
> called "priority".  Normally in C++, you get a naming collision if you
> try to make a member variable and member function the same name.  I
> notice, however, that the member variable isn't declared here.  The
> implementation of the getter and setter functions isn't given for this
> example, and I'm confused about how to write them.  If I try to do
> something like "this->priority = priority;" for the setter, or "return
> priority;" for the getter, I get errors indicating that the compiler
> thinks I am referring to the member function not the variable.  If I
> try to declare the Priority priority in the header file, I get errors
> about naming conflict as expected.  What is the proper way to do this?
> 
> Thanks,
> Colin Kern

Hi Colin, 

I think the name of the property in the Q_PROPERTY has nothing to do with
the member variable. So you usually just declare a private variable:

private:
	Priority mPriority;

and the property methods access that variable.  Note that the property
methods may not require a member variable at all.  The name in the property
macro is displayed in designer, and used in QObject::property / setProperty.


Hope that helps, 





More information about the Qt-interest-old mailing list