QQmlProperty Class

The QQmlProperty class abstracts accessing properties on objects created from QML. More...

Header: #include <QQmlProperty>
qmake: QT += qml
Since: Qt 5.0

This class was introduced in Qt 5.0.

Public Types

enum PropertyTypeCategory { InvalidCategory, List, Object, Normal }
enum Type { Invalid, Property, SignalProperty }

Detailed Description

As QML uses Qt's meta-type system all of the existing QMetaObject classes can be used to introspect and interact with objects created by QML. However, some of the new features provided by QML - such as type safety and attached properties - are most easily used through the QQmlProperty class that simplifies some of their natural complexity.

Unlike QMetaProperty which represents a property on a class type, QQmlProperty encapsulates a property on a specific object instance. To read a property's value, programmers create a QQmlProperty instance and call the read() method. Likewise to write a property value the write() method is used.

For example, for the following QML code:

 // MyItem.qml
 import QtQuick 2.0

 Text { text: "A bit of text" }

The Text object's properties could be accessed using QQmlProperty, like this:

 #include <QQmlProperty>
 #include <QGraphicsObject>

 ...

 QQuickView view(QUrl::fromLocalFile("MyItem.qml"));
 QQmlProperty property(view.rootObject(), "font.pixelSize");
 qWarning() << "Current pixel size:" << property.read().toInt();
 property.write(24);
 qWarning() << "Pixel size should now be 24:" << property.read().toInt();

Member Type Documentation

enum QQmlProperty::PropertyTypeCategory

This enum specifies a category of QML property.

ConstantValueDescription
QQmlProperty::InvalidCategory0The property is invalid, or is a signal property.
QQmlProperty::List1The property is a QQmlListProperty list property
QQmlProperty::Object2The property is a QObject derived type pointer
QQmlProperty::Normal3The property is a normal value property.

enum QQmlProperty::Type

This enum specifies a type of QML property.

ConstantValueDescription
QQmlProperty::Invalid0The property is invalid.
QQmlProperty::Property1The property is a regular Qt property.
QQmlProperty::SignalProperty2The property is a signal property.