[Qt-qml] QDeclarativeProperty and custom qml types.

Charley Bay charleyb123 at gmail.com
Sat Oct 23 19:20:57 CEST 2010


Simon spaketh:
> I'm having trouble with assigning qml item properties in C++.  The
> type is declared in qml as a component, and is declared in another
> qml component as an item property.
>
> <snip, QML and C++ code>

> However when I write the value, using :
> ...
> QObject *myTypeA = component.create()
> typeAProperty.write(QVariant::fromValue(myTypeA));
>
> .. it returns false which I guess means the type is incorrect according
> to the metatype, even though the QVariant's type is also QVariant::QObject*.

I don't see anything wrong here -- are you sure "MyTypeA" has
Q_OBJECT in the header and went through "moc"?

> <snip>,
> I couldn't find any examples in the documentation that specifically
> dealt with item properties.

You probably saw the example at:

<http://doc.qt.nokia.com/4.7/qml-extending.html>

...it instantiates custom types as properties, but you're correct,
it doesn't specifically define the properties in a QML extended
type (they were defined as properties in C++, not as you did in
your QML code).  For example, the Trolls may consider adding
an example like:

Item {
  property MyCustomTypeFromCpp myProperty
  property MyCustomTypeFromCpp2 myProperty2: { ...init code... }
}

>I've managed to get lists to work in a similar manner using
> QDeclarativeListReference, but the convention for item properties
> seems subtly different.

I've noticed this too, but it could be my (mis-)understanding
related to some of the "dynamic scoping" issues.  (For example,
a double-alias-reference in the QML item definition won't resolve
due to dynamic scoping from the double-indirection).

I'd double-check that Q_OBJECT in your custom type header,
the type registration code, and that your type was moc'd in
your build.

Good luck?  ;-)

--charley



More information about the Qt-qml mailing list