[Qt-qml] Accessing dynamic properties

aaron.kennedy at nokia.com aaron.kennedy at nokia.com
Wed Sep 15 06:02:32 CEST 2010


Hi,

No it doesn't work, and no its not supposed to work.  Qt's dynamic
properties do not fit the QML model very well which relies on knowing the
complete static type of an object prior to instantiation, and we'd prefer
not to support them.

Perhaps if you give more details about the actual problem you are trying to
solve we can think of an alternative way.

Cheers,

Aaron


On 15/09/10 2:58 AM, "Dzyubenko Denis (Nokia-MS-Qt/Oslo)"
<denis.dzyubenko at nokia.com> wrote:

> 
> On 14. sep. 2010, at 18.20, Hicks Jamey (Nokia-NRC/Cambridge) wrote:
> 
>> How did you pass in the object? If you pass it as a QVariant you need to cast
>> it to QObject * first to make its slots and properties available.
> 
> I've created a test app that shows the problem. Maybe someone can tell me if
> this is supposed to work at all:
> 
> #include <QtGui>
> #include <QtDeclarative>
> 
> class MyItem: public QDeclarativeItem
> {
>     Q_OBJECT
>     Q_PROPERTY(int bar READ bar WRITE setBar)
> 
> public:
>     MyItem()
>         : m_bar(69)
>     {
>         setProperty("foo", QVariant::fromValue<int>(42));
>         setProperty("bar", QVariant::fromValue<int>(43));
>     }
> 
>     int m_bar;
>     int bar() const  { return m_bar; }
>     void setBar(int value) { m_bar = value; }
> };
> QML_DECLARE_TYPE(MyItem);
> 
> int main(int argc, char **argv)
> {
>     QApplication app(argc, argv);
>     qmlRegisterType<MyItem>("org.foobar.foo", 1, 0, "MyItem");
>     QDeclarativeView view;
> 
>     view.setSource(QUrl::fromLocalFile("foo.qml"));
> 
>     view.show();
>     return app.exec();
> }
> 
> #include "main.moc"
> 
> 
> 
> and the foo.qml:
> 
> import Qt 4.7
> import org.foobar.foo 1.0
> 
> MyItem {
>     width: 100
>     height: 100
> 
>     MouseArea {
>         anchors.fill: parent
>         onClicked: { console.log("foo =", parent.foo, "; bar =", parent.bar);
> }
>     }
> }
> 
> 
> I get the following output:
> 
> foo = undefined ; bar = 43
> 
> Denis.
> 
> 
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-qml





More information about the Qt-qml mailing list