[Interest] QQmlPropertyList x QList in hybrid C++/QML APIs

Sandro Andrade sandroandrade at kde.org
Fri Jun 14 20:49:52 CEST 2013


On Fri, Jun 14, 2013 at 2:48 PM, Alan Alpert <416365416c at gmail.com> wrote:
> To use types across QML/C++ you should use qmlRegisterType.
> QObject-based types aren't automatically registered with QML, but
> certain things may still work. element.ownedAttributes is undefined
> because you haven't registered the type.
>

Ok, I've confirm that, but it seems that properties with type QList<T *>
still are handled as QVariant(QList<T *>), even if T is registered in QML.
It works only if property type is QVariantList, is that right ?

Changing all my properties to QVariantList would imply a somehow weaker
type safety in my API :(

> However you don't have to maintain two different classes. You can just
> register the same C++ class into QML. To interact with the list
> property properly (mostly just assigning to it in QML, i.e. UmlClass {
> ownedOperations: [UmlOperation {}] } ) you need to have a
> QQmlListProperty type property, not just the QList<QObject*> property.
> But the QQmlListProperty type is basically just a wrapper around a
> QList<QObject*> (providing better control of the allocation if you
> need it), so it will be using the same QList<QObject*> as the data.
> You just need the extra property, not duplicating or synchronizing
> data, so it's easy to put it on the same C++ class and there's very
> little more you have do to use it properly in QML.

Ok, but that requires a new property name to prevent name clashing,
doesn't it ? I'd have to have something like:

Q_PROPERTY(QList<QUmlProperty *> ownedAttributes READ ownedAttributes)
and
Q_PROPERTY(QQmlListProperty<QUmlProperty *> qmlOwnedAttributes READ
ownedAttributes)

Thanks for help,
Sandro

>
> --
> Alan Alpert
>
> On Fri, Jun 14, 2013 at 9:15 AM, Sandro Andrade <sandroandrade at kde.org> wrote:
>> Hi there,
>>
>> I've a bunch of classes which define a number of Q_PROPERTies with
>> type QList<some-QObject-based-class *>. Those classes are inspected by
>> a generic widgets-based property editor which uses QMetaProperty
>> functions to handle item's properties.
>>
>> E.g:
>>
>> class Q_UML_EXPORT QUmlClass : public QWrappedObject
>> {
>>     Q_OBJECT
>>     Q_CLASSINFO("MetaModelPrefix", "QUml")
>> ...
>>     Q_PROPERTY(QList<QUmlOperation *> ownedOperations READ ownedOperations)
>>     Q_PROPERTY(QList<QUmlProperty *> ownedAttributes READ ownedAttributes)
>> ...
>>
>> Now, I need to use QML to provide some visual representation of those
>> same elements and, therefore, QML objects should be dynamically
>> created based on QList-based properties' contents:
>>
>> import QtQuick 2.0
>>
>> Rectangle {
>> ...
>>     UmlSlot {
>>         id: nameSlot
>>     }
>>     UmlSlot {
>>         id: propertiesSlot
>>         anchors.top: nameSlot.bottom
>>         anchors.topMargin: -1
>>         height: nameSlot.height; width: nameSlot.width
>>     }
>> ...
>>     Component.onCompleted: {
>>         if (element) {
>>             var visibility;
>>             switch (element.visibility) {
>>                 case 0: visibility = "+"; break;
>>                 case 1: visibility = "-"; break;
>>                 case 2: visibility = "#"; break;
>>                 case 3: visibility = "~"; break;
>>             }
>>             name = visibility + element.objectName;
>>             nameSlot.labelFont.italic = element.isAbstract; // Works
>> fine so far ...
>>             element.ownedAttributes <- here I got an undefined object
>>         }
>>     }
>> }
>>
>> Qt5 docs say that such properties should be declared as
>> QQmlListProperty, but I'm wondering if I have to maintain two
>> different classes/properties in order to have this working. I haven't
>> used qmlRegisterType() so far, it seems QObject-based type are
>> automatically registered in QML as metatype registration do in Qt5.
>>
>> So, any hints on how to have such QML/C++ things living together ?
>>
>> Thanks in advance,
>> --
>> Sandro
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/interest



More information about the Interest mailing list