[Qt-interest] Q_PROPERTY confusion

Bill KING bill.king at trolltech.com
Mon Jun 22 04:07:42 CEST 2009


Scott Aron Bloom wrote:
> the
>
>   
>> -----Original Message-----
>> From: qt-interest-bounces at trolltech.com [mailto:qt-interest-
>> bounces at trolltech.com] On Behalf Of Colin Kern
>> Sent: Sunday, June 21, 2009 4:59 PM
>> To: qt-interest at trolltech.com
>> Subject: [Qt-interest] Q_PROPERTY confusion
>>
>> 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
>>     
>
>
> ------------
> The name is NOT used as an actual object inside the class.. Its used by
> the metaObject from TT..
>
> It's the key to the mapping system used to lookup the properties.
>
> Scott
>
>
>
>
>   
Scott is correct here, it's used for the introspection functionality in
Qt's meta system. The upside of that is that it automatically makes it
ready for the scripting(/DUI) systems. (Ie, it describes this as a
property, which in those systems, will act exactly like a smart member
variable).


-- 
Bill King, Software Engineer
Qt Software, Nokia Pty Ltd
Brisbane Office




More information about the Qt-interest-old mailing list