QML:QtObject Element Reference

The QtObject element is the most basic element in QML.

[QML:QtObject instantiates the C++ class QObject]

This element was introduced in Qt 4.7.

Detailed Description

The QtObject element is the most basic element in QML.

The QtObject element is a non-visual element which contains only the objectName property.

It can be useful to create a QtObject if you need an extremely lightweight element to enclose a set of custom properties:

  1. import QtQuick 1.0
  2.  
  3. Item  {
  4.     QtObject  {
  5.         id: attributes
  6.         property string name
  7.         property int size
  8.         property variant attributes
  9.     }
  10.  
  11.     Text  { text: attributes.name }
  12. }

It can also be useful for C++ integration, as it is just a plain QObject. See the QObject documentation for further details.

See also QMetaObject, QPointer, QObjectCleanupHandler, Q_DISABLE_COPY(), and Object Trees & Ownership.

Properties

Property Documentation

This property holds the QObject::objectName for this specific object instance.

This allows a C++ application to locate an item within a QML component using the QObject::findChild() method. For example, the following C++ application locates the child Rectangle item and dynamically changes its color value:

  1. // MyRect.qml
  2.  
  3. import QtQuick 1.0
  4.  
  5. Item  {
  6.     width: 200; height: 200
  7.  
  8.     Rectangle  {
  9.         anchors.fill: parent
  10.         color: "red"
  11.         objectName: "myRect"
  12.     }
  13. }
  1. // main.cpp
  2.  
  3. QDeclarativeView view;
  4. view.setSource(QUrl::fromLocalFile("MyRect.qml"));
  5. view.show();
  6.  
  7. QDeclarativeItem *item = view.rootObject()->findChild<QDeclarativeItem*>("myRect");
  8. if (item)
  9.     item->setProperty("color", QColor(Qt::yellow));
Notes provided by the Qt Community

No notes