[Qt-qml] QML C++ Plugin template in Qt Creator

Thorbjørn Lindeijer thorbjorn.lindeijer at nokia.com
Wed Jan 26 12:16:35 CET 2011


On 01/25/2011 03:29 PM, ext Jamil Naja wrote:
> Isn't there a contradiction between the disabling the copy constructor
> and declaring  the class as a MetaType (done by QML_DECLARE_TYPE)  ?
>
> The documentation
> <http://doc.trolltech.com/latest/qmetatype.html#Q_DECLARE_METATYPE>says
> that types that are know to QMetaType should have a public copy constructor.
>
> classMyItem:publicQDeclarativeItem
> {
> .....
> Q_DISABLE_COPY(MyItem)
> .....
> };
> QML_DECLARE_TYPE(MyItem)

Apparently there is a small bug in the documentation of QML_DECLARE_TYPE 
(I'm having trouble finding the source of these docs), in that it fails 
to mention that it declares a pointer to the type as meta type rather 
than the type itself. This means the "copy constructor" is implicitly 
present as it is for all pointers.

#define QML_DECLARE_TYPE(TYPE) \
     Q_DECLARE_METATYPE(TYPE *) \
     Q_DECLARE_METATYPE(QDeclarativeListProperty<TYPE>)

So there is in fact no contradiction, but a missing * in the docs. :)

> It would be great if you explain why these two lines are added to the
> generated code as well and what are the implications of not having them,
> especially since the tutorial
> <http://doc.qt.nokia.com/latest/declarative-tutorials-extending-chapter6-plugins.html>on
> how to create an extension plugin uses neither of those macros

The Q_DISABLE_COPY is just to make it explicit that the class can't be 
copied (it naturally can't since QObject can't be copied - and already 
uses that macro itself).

I don't know exactly in which situations you need the QML_DECLARE_TYPE. 
I know I'm personally using custom types and even 
QDeclarativeListProperty of custom types without actually using 
QML_DECLARE_TYPE. Apparently it's to allow the pointer to the type to be 
stored in a QVariant, which I guess is only relevant if you're actually 
doing that in your C++ code.

Best regards,
Bjørn

-- 
Thorbjørn Lindeijer
Software Engineer
Nokia, Qt Development Frameworks


More information about the Qt-qml mailing list