[Qt-interest] qRegisterMetaType and namespaces

Andre Somers andre at familiesomers.nl
Fri Sep 4 15:22:55 CEST 2009


Thiago Macieira wrote:
> Em Sexta-feira 04 Setembro 2009, às 14:34:15, Andre Somers escreveu:
>   
>>  Q_FLAGS(Options)
>>
>> "Must be used"? Where? How? In a header, or in the implementation? Does
>> this expand to anything, or is it supposed to be trigger moc somehow?
>> And, more importantly in my case, not a word about stuff like namespaces.
>>
>> Anybody any suggestions for the right incantation that I need to chant?
>>     
>
> In the header, in the class, along with Q_OBJECT and any other eventual 
> Q_ENUM, Q_CLASSINFO and Q_PROPERTY.
>   
The type is defined outside a class, but inside the namespace.
The header that defines the type looks like this:

namespace PropertyEditor

{

enum SelectionPolicy {

ManualOnly = 0x00,

SuperClassProperties = 0x02,

LocalProperties = 0x04,

VisibilityHints = 0x08,

AllowedList = 0x10,

AllProperties = SuperClassProperties | LocalProperties

};

Q_DECLARE_FLAGS(SelectionPolicyFlags, SelectionPolicy)

Q_FLAGS(SelectionPolicyFlags)



class PropertyModel : public QAbstractItemModel

{

Q_OBJECT

public:
   // the rest of the definition is here

}; // end of class definition


Q_DECLARE_OPERATORS_FOR_FLAGS(PropertyEditor::SelectionPolicyFlags)

} // end of namespace

Q_DECLARE_METATYPE(PropertyEditor::SelectionPolicyFlags)



Then, there is another header, that includes the header file above, that 
looks like this:



class PropertyEditor : public QTreeView

{

Q_OBJECT

Q_PROPERTY (QStringList visibleProperties READ visibleProperties WRITE 
setVisibleProperties DESIGNABLE true)

Q_PROPERTY (QStringList allowedProperties READ allowedProperties WRITE 
setAllowedProperties DESIGNABLE true)

Q_PROPERTY (SelectionPolicyFlags selectionPolicy READ selectionPolicy 
WRITE setSelectionPolicy DESIGNABLE true)

public:

PropertyEditor(QWidget *parent = 0);


//etc.
};



The qDeclareMetatype issue has been solved with the help of Girish 
above; it is now successfully called by the constructor of the 
PropertyEditor class. Great. That makes sure the selectionPolicy above 
actually works as property.

So far, I have not been able to find a place to put the Q_FLAGS and have 
it actually make the selectionPolicy property be recognized as a 
flag-type. The property's QMetaProperty::isFlagType() keeps returning 
false. I have tried permutations where I:
* prepend the SelectionPolicyFlags with the namespace name or not
* put the Q_FLAGS right under the Q_DECLARE_FLAGS (as above) or under 
the Q_OBJECT in the PropertyModel definition or put it with the Q_OBJECT 
in the PropertyEditor class header.


Is there something I have overlooked? Thanks.


André






More information about the Qt-interest-old mailing list