[Qt-qml] how can I change the property of QML element in C++ application?

michael.brasser at nokia.com michael.brasser at nokia.com
Mon Aug 30 02:49:20 CEST 2010


On 27/08/2010, at 6:52 PM, ext Wei, James wrote:

I have a QML file and I want to integrate it into a graphics view framework based UI application written in C++.

   QDeclarativeEngine *engine = new QDeclarativeEngine;
   QDeclarativeComponent component(engine, QUrl::fromLocalFile("Button.qml"));
   QGraphicsObject *object =
           qobject_cast<QGraphicsObject *>(component.create());
   scene->addItem(object);

How can I change the property of the element in C++ application?

Hi James,

You could use Qt's property system to manipulate the item (see http://doc.qt.nokia.com/4.7-snapshot/properties.html#reading-and-writing-properties-with-the-meta-object-system for more on using the property system). For example, assuming the above "Button.qml" had a Rectangle as the root, you could change its color with object->setProperty("color", QColor("green"));

You might also consider an architecture where you never manipulate the item directly from C++; instead, expose the relevant properties/signals/slots from C++ and write the QML to react accordingly (as described in http://doc.qt.nokia.com/4.7-snapshot/qtbinding.html#exposing-data).

Regards,
Michael

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt.nokia.com/pipermail/qt-qml/attachments/20100830/0b714916/attachment.html 


More information about the Qt-qml mailing list