QQuickItem Class
The QQuickItem class provides the most basic of all visual items in Qt Quick. More...
| Header: | #include <QQuickItem> |
| qmake: | QT += quick |
| Instantiated By: | Item |
| Inherits: | QObject and QQmlParserStatus |
| Inherited By: |
Public Types
| union | ItemChangeData |
| enum | Flag { ItemClipsChildrenToShape, ItemAcceptsInputMethod, ItemIsFocusScope, ItemHasContents, ItemAcceptsDrops } |
| enum | ItemChange { ItemChildAddedChange, ItemChildRemovedChange, ItemSceneChange, ItemVisibleHasChanged, ItemParentHasChanged, …, ItemEnabledHasChanged } |
| enum | TransformOrigin { TopLeft, Top, TopRight, Left, Center, …, BottomRight } |
Properties
|
Public Functions
| bool | activeFocusOnTab() const |
| bool | antialiasing() const |
| qreal | baselineOffset() const |
| QRectF | childrenRect() |
| bool | clip() const |
| QObject * | containmentMask() const |
| bool | hasActiveFocus() const |
| bool | hasFocus() const |
| qreal | height() const |
| qreal | implicitHeight() const |
| qreal | implicitWidth() const |
| bool | isEnabled() const |
| bool | isVisible() const |
| qreal | opacity() const |
| QQuickItem * | parentItem() const |
| void | resetAntialiasing() |
| void | resetHeight() |
| void | resetWidth() |
| qreal | rotation() const |
| qreal | scale() const |
| void | setActiveFocusOnTab(bool) |
| void | setAntialiasing(bool) |
| void | setBaselineOffset(qreal) |
| void | setClip(bool) |
| void | setContainmentMask(QObject *mask) |
| void | setEnabled(bool) |
| void | setFocus(bool) |
| void | setFocus(bool focus, Qt::FocusReason reason) |
| void | setHeight(qreal) |
| void | setImplicitHeight(qreal) |
| void | setImplicitWidth(qreal) |
| void | setOpacity(qreal) |
| void | setParentItem(QQuickItem *parent) |
| void | setRotation(qreal) |
| void | setScale(qreal) |
| void | setSmooth(bool) |
| void | setState(const QString &) |
| void | setTransformOrigin(QQuickItem::TransformOrigin) |
| void | setVisible(bool) |
| void | setWidth(qreal) |
| void | setX(qreal) |
| void | setY(qreal) |
| void | setZ(qreal) |
| bool | smooth() const |
| QString | state() const |
| QQuickItem::TransformOrigin | transformOrigin() const |
| qreal | width() const |
| qreal | x() const |
| qreal | y() const |
| qreal | z() const |
Signals
| void | activeFocusChanged(bool) |
| void | activeFocusOnTabChanged(bool) |
| void | antialiasingChanged(bool) |
| void | baselineOffsetChanged(qreal) |
| void | childrenRectChanged(const QRectF &) |
| void | clipChanged(bool) |
| void | containmentMaskChanged() |
| void | enabledChanged() |
| void | focusChanged(bool) |
| void | heightChanged() |
| void | implicitHeightChanged() |
| void | implicitWidthChanged() |
| void | opacityChanged() |
| void | parentChanged(QQuickItem *) |
| void | rotationChanged() |
| void | scaleChanged() |
| void | smoothChanged(bool) |
| void | stateChanged(const QString &) |
| void | transformOriginChanged(QQuickItem::TransformOrigin) |
| void | visibleChanged() |
| void | widthChanged() |
| void | xChanged() |
| void | yChanged() |
| void | zChanged() |
Detailed Description
All visual items in Qt Quick inherit from QQuickItem. Although a QQuickItem instance has no visual appearance, it defines all the attributes that are common across visual items, such as x and y position, width and height, anchoring and key handling support.
You can subclass QQuickItem to provide your own custom visual item that inherits these features.
Custom Scene Graph Items
All visual QML items are rendered using the scene graph, the default implementation of which is a low-level, high-performance rendering stack, closely tied to OpenGL. It is possible for subclasses of QQuickItem to add their own custom content into the scene graph by setting the QQuickItem::ItemHasContents flag and reimplementing the QQuickItem::updatePaintNode() function.
Warning: It is crucial that OpenGL operations and interaction with the scene graph happens exclusively on the rendering thread, primarily during the updatePaintNode() call. The best rule of thumb is to only use classes with the "QSG" prefix inside the QQuickItem::updatePaintNode() function.
Note: All classes with QSG prefix should be used solely on the scene graph's rendering thread. See Scene Graph and Rendering for more information.
Graphics Resource Handling
The preferred way to handle cleanup of graphics resources used in the scene graph, is to rely on the automatic cleanup of nodes. A QSGNode returned from QQuickItem::updatePaintNode() is automatically deleted on the right thread at the right time. Trees of QSGNode instances are managed through the use of QSGNode::OwnedByParent, which is set by default. So, for the majority of custom scene graph items, no extra work will be required.
Implementations that store graphics resources outside the node tree, such as an item implementing QQuickItem::textureProvider(), will need to take care in cleaning it up correctly depending on how the item is used in QML. The situations to handle are:
- The scene graph is invalidated; This can happen, for instance, if the window is hidden using QQuickWindow::hide(). If the item class implements a
slotnamedinvalidateSceneGraph(), this slot will be called on the rendering thread while the GUI thread is blocked. This is equivalent to connecting to QQuickWindow::sceneGraphInvalidated(). The OpenGL context of this item's window will be bound when this slot is called. The only exception is if the native OpenGL has been destroyed outside Qt's control, for instance throughEGL_CONTEXT_LOST. - The item is removed from the scene; If an item is taken out of the scene, for instance because it's parent was set to
nullor an item in another window, the QQuickItem::releaseResources() will be called on the GUI thread. QQuickWindow::scheduleRenderJob() should be used to schedule cleanup of rendering resources. - The item is deleted; When the destructor if an item runs, it should delete any graphics resources it has. If neither of the two conditions above were already met, the item will be part of a window and it is possible to use QQuickWindow::scheduleRenderJob() to have them cleaned up. If an implementation ignores the call to QQuickItem::releaseResources(), the item will in many cases no longer have access to a QQuickWindow and thus no means of scheduling cleanup.
When scheduling cleanup of graphics resources using QQuickWindow::scheduleRenderJob(), one should use either QQuickWindow::BeforeSynchronizingStage or QQuickWindow::AfterSynchronizingStage. The synchronization stage is where the scene graph is changed as a result of changes to the QML tree. If cleanup is scheduled at any other time, it may result in other parts of the scene graph referencing the newly deleted objects as these parts have not been updated.
Note: Use of QObject::deleteLater() to clean up graphics resources is not recommended as this will run at an arbitrary time and it is unknown if there will be an OpenGL context bound when the deletion takes place.
Custom QPainter Items
The QQuickItem provides a subclass, QQuickPaintedItem, which allows the users to render content using QPainter.
Warning: Using QQuickPaintedItem uses an indirect 2D surface to render its content, either using software rasterization or using an OpenGL framebuffer object (FBO), so the rendering is a two-step operation. First rasterize the surface, then draw the surface. Using scene graph API directly is always significantly faster.
Behavior Animations
If your Item uses the Behavior type to define animations for property changes, you should always use either QObject::setProperty(), QQmlProperty(), or QMetaProperty::write() when you need to modify those properties from C++. This ensures that the QML engine knows about the property change. Otherwise, the engine won't be able to carry out your requested animation. Note that these functions incur a slight performance penalty. For more details, see Accessing Members of a QML Object Type from C++.
See also QQuickWindow and QQuickPaintedItem.
Member Type Documentation
enum QQuickItem::Flag
This enum type is used to specify various item properties.
| Constant | Value | Description |
|---|---|---|
QQuickItem::ItemClipsChildrenToShape | 0x01 | Indicates this item should visually clip its children so that they are rendered only within the boundaries of this item. |
QQuickItem::ItemAcceptsInputMethod | 0x02 | Indicates the item supports text input methods. |
QQuickItem::ItemIsFocusScope | 0x04 | Indicates the item is a focus scope. See Keyboard Focus in Qt Quick for more information. |
QQuickItem::ItemHasContents | 0x08 | Indicates the item has visual content and should be rendered by the scene graph. |
QQuickItem::ItemAcceptsDrops | 0x10 | Indicates the item accepts drag and drop events. |
See also setFlag(), setFlags(), and flags().
enum QQuickItem::ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
| Constant | Value | Description |
|---|---|---|
QQuickItem::ItemChildAddedChange | 0 | A child was added. ItemChangeData::item contains the added child. |
QQuickItem::ItemChildRemovedChange | 1 | A child was removed. ItemChangeData::item contains the removed child. |
QQuickItem::ItemSceneChange | 2 | The item was added to or removed from a scene. The QQuickWindow rendering the scene is specified in using ItemChangeData::window. The window parameter is null when the item is removed from a scene. |
QQuickItem::ItemVisibleHasChanged | 3 | The item's visibility has changed. ItemChangeData::boolValue contains the new visibility. |
QQuickItem::ItemParentHasChanged | 4 | The item's parent has changed. ItemChangeData::item contains the new parent. |
QQuickItem::ItemOpacityHasChanged | 5 | The item's opacity has changed. ItemChangeData::realValue contains the new opacity. |
QQuickItem::ItemActiveFocusHasChanged | 6 | The item's focus has changed. ItemChangeData::boolValue contains whether the item has focus or not. |
QQuickItem::ItemRotationHasChanged | 7 | The item's rotation has changed. ItemChangeData::realValue contains the new rotation. |
QQuickItem::ItemDevicePixelRatioHasChanged | 9 | The device pixel ratio of the screen the item is on has changed. ItemChangedData::realValue contains the new device pixel ratio. |
QQuickItem::ItemAntialiasingHasChanged | 8 | The antialiasing has changed. The current (boolean) value can be found in QQuickItem::antialiasing. |
QQuickItem::ItemEnabledHasChanged | 10 | The item's enabled state has changed. ItemChangeData::boolValue contains the new enabled state. (since Qt 5.10) |
enum QQuickItem::TransformOrigin
Controls the point about which simple transforms like scale apply.
| Constant | Value | Description |
|---|---|---|
QQuickItem::TopLeft | 0 | The top-left corner of the item. |
QQuickItem::Top | 1 | The center point of the top of the item. |
QQuickItem::TopRight | 2 | The top-right corner of the item. |
QQuickItem::Left | 3 | The left most point of the vertical middle. |
QQuickItem::Center | 4 | The center of the item. |
QQuickItem::Right | 5 | The right most point of the vertical middle. |
QQuickItem::BottomLeft | 6 | The bottom-left corner of the item. |
QQuickItem::Bottom | 7 | The center point of the bottom of the item. |
QQuickItem::BottomRight | 8 | The bottom-right corner of the item. |
See also transformOrigin() and setTransformOrigin().
Property Documentation
Defines the natural width or height of the Item if no width or height is specified.
The default implicit size for most items is 0x0, however some items have an inherent implicit size which cannot be overridden, for example, Image and Text.
Setting the implicit size is useful for defining components that have a preferred size based on their content, for example:
// Label.qml import QtQuick 2.0 Item { property alias icon: image.source property alias label: text.text implicitWidth: text.implicitWidth + image.implicitWidth implicitHeight: Math.max(text.implicitHeight, image.implicitHeight) Image { id: image } Text { id: text wrapMode: Text.Wrap anchors.left: image.right; anchors.right: parent.right anchors.verticalCenter: parent.verticalCenter } }
Note: Using implicitWidth of Text or TextEdit and setting the width explicitly incurs a performance penalty as the text must be laid out twice.
activeFocus : const bool
This read-only property indicates whether the item has active focus.
If activeFocus is true, either this item is the one that currently receives keyboard input, or it is a FocusScope ancestor of the item that currently receives keyboard input.
Usually, activeFocus is gained by setting focus on an item and its enclosing FocusScope objects. In the following example, the input and focusScope objects will have active focus, while the root rectangle object will not.
import QtQuick 2.0 Rectangle { width: 100; height: 100 FocusScope { focus: true TextInput { id: input focus: true } } }
Access functions:
| bool | hasActiveFocus() const |
Notifier signal:
| void | activeFocusChanged(bool) |
See also focus and Keyboard Focus in Qt Quick.
activeFocusOnTab : bool
This property holds whether the item wants to be in the tab focus chain. By default, this is set to false.
Access functions:
| bool | activeFocusOnTab() const |
| void | setActiveFocusOnTab(bool) |
Notifier signal:
| void | activeFocusOnTabChanged(bool) |
antialiasing : bool
Specifies whether the item is antialiased or not
Used by visual elements to decide if the item should use antialiasing or not. In some cases items with antialiasing require more memory and are potentially slower to render (see Antialiasing for more details).
The default is false, but may be overridden by derived elements.
Access functions:
| bool | antialiasing() const |
| void | setAntialiasing(bool) |
| void | resetAntialiasing() |
Notifier signal:
| void | antialiasingChanged(bool) |
baselineOffset : qreal
Specifies the position of the item's baseline in local coordinates.
The baseline of a Text item is the imaginary line on which the text sits. Controls containing text usually set their baseline to the baseline of their text.
For non-text items, a default baseline offset of 0 is used.
Access functions:
| qreal | baselineOffset() const |
| void | setBaselineOffset(qreal) |
Notifier signal:
| void | baselineOffsetChanged(qreal) |
childrenRect : const QRectF
This property holds the collective position and size of the item's children.
This property is useful if you need to access the collective geometry of an item's children in order to correctly size the item.
The geometry that is returned is local to the item. For example:
Item { x: 50 y: 100 // prints: QRectF(-10, -20, 30, 40) Component.onCompleted: print(childrenRect) Item { x: -10 y: -20 width: 30 height: 40 } }
Access functions:
| QRectF | childrenRect() |
Notifier signal:
| void | childrenRectChanged(const QRectF &) |
clip : bool
This property holds whether clipping is enabled. The default clip value is false.
If clipping is enabled, an item will clip its own painting, as well as the painting of its children, to its bounding rectangle. If you set clipping during an item's paint operation, remember to re-set it to prevent clipping the rest of your scene.
Note: Clipping can affect rendering performance. See Clipping for more information.
Access functions:
| bool | clip() const |
| void | setClip(bool) |
Notifier signal:
| void | clipChanged(bool) |
containmentMask : QObject*
This property holds an optional mask to be used in the contains() method, which is mainly used for hit-testing each QPointerEvent.
By default, contains() will return true for any point within the Item's bounding box. But any QQuickItem, or any QObject that implements a function of the form
Q_INVOKABLE bool contains(const QPointF &point) const;
can be used as a mask, to defer hit-testing to that object.
Note: contains() is called frequently during event delivery. Deferring hit-testing to another object slows it down somewhat. containmentMask() can cause performance problems if that object's contains() method is not efficient. If you implement a custom QQuickItem subclass, you can alternatively override contains().
This property was introduced in Qt 5.11.
Access functions:
| QObject * | containmentMask() const |
| void | setContainmentMask(QObject *mask) |
Notifier signal:
| void | containmentMaskChanged() |
See also contains().
enabled : bool
This property holds whether the item receives mouse and keyboard events. By default this is true.
Setting this property directly affects the enabled value of child items. When set to false, the enabled values of all child items also become false. When set to true, the enabled values of child items are returned to true, unless they have explicitly been set to false.
Setting this property to false automatically causes activeFocus to be set to false, and this item will longer receive keyboard events.
Access functions:
| bool | isEnabled() const |
| void | setEnabled(bool) |
Notifier signal:
| void | enabledChanged() |
See also visible.
focus : bool
This property holds whether the item has focus within the enclosing FocusScope. If true, this item will gain active focus when the enclosing FocusScope gains active focus.
In the following example, input will be given active focus when scope gains active focus:
import QtQuick 2.0 Rectangle { width: 100; height: 100 FocusScope { id: scope TextInput { id: input focus: true } } }
For the purposes of this property, the scene as a whole is assumed to act like a focus scope. On a practical level, that means the following QML will give active focus to input on startup.
Rectangle { width: 100; height: 100 TextInput { id: input focus: true } }
Access functions:
| bool | hasFocus() const |
| void | setFocus(bool) |
| void | setFocus(bool focus, Qt::FocusReason reason) |
Notifier signal:
| void | focusChanged(bool) |
See also activeFocus and Keyboard Focus in Qt Quick.
height : qreal
This property holds the height of this item.
Access functions:
| qreal | height() const |
| void | setHeight(qreal) |
| void | resetHeight() |
Notifier signal:
| void | heightChanged() |
opacity : qreal
This property holds the opacity of the item. Opacity is specified as a number between 0.0 (fully transparent) and 1.0 (fully opaque). The default value is 1.0.
When this property is set, the specified opacity is also applied individually to child items. This may have an unintended effect in some circumstances. For example in the second set of rectangles below, the red rectangle has specified an opacity of 0.5, which affects the opacity of its blue child rectangle even though the child has not specified an opacity.
Values outside the range of 0 to 1 will be clamped.
| Item { Rectangle { color: "red" width: 100; height: 100 Rectangle { color: "blue" x: 50; y: 50; width: 100; height: 100 } } } |
| Item { Rectangle { opacity: 0.5 color: "red" width: 100; height: 100 Rectangle { color: "blue" x: 50; y: 50; width: 100; height: 100 } } } |
Changing an item's opacity does not affect whether the item receives user input events. (In contrast, setting visible property to false stops mouse events, and setting the enabled property to false stops mouse and keyboard events, and also removes active focus from the item.)
Access functions:
| qreal | opacity() const |
| void | setOpacity(qreal) |
Notifier signal:
| void | opacityChanged() |
See also visible.
parent : QQuickItem*
This property holds the visual parent of the item.
Note: The concept of the visual parent differs from that of the QObject parent. An item's visual parent may not necessarily be the same as its object parent. See Concepts - Visual Parent in Qt Quick for more details.
Access functions:
| QQuickItem * | parentItem() const |
| void | setParentItem(QQuickItem *parent) |
Notifier signal:
| void | parentChanged(QQuickItem *) |
rotation : qreal
This property holds the rotation of the item in degrees clockwise around its transformOrigin.
The default value is 0 degrees (that is, no rotation).
| Rectangle { color: "blue" width: 100; height: 100 Rectangle { color: "red" x: 25; y: 25; width: 50; height: 50 rotation: 30 } } |
Access functions:
| qreal | rotation() const |
| void | setRotation(qreal) |
Notifier signal:
| void | rotationChanged() |
See also Transform and Rotation.
scale : qreal
This property holds the scale factor for this item.
A scale of less than 1.0 causes the item to be rendered at a smaller size, and a scale greater than 1.0 renders the item at a larger size. A negative scale causes the item to be mirrored when rendered.
The default value is 1.0.
Scaling is applied from the transformOrigin.
| import QtQuick 2.0 Rectangle { color: "blue" width: 100; height: 100 Rectangle { color: "green" width: 25; height: 25 } Rectangle { color: "red" x: 25; y: 25; width: 50; height: 50 scale: 1.4 } } |
Access functions:
| qreal | scale() const |
| void | setScale(qreal) |
Notifier signal:
| void | scaleChanged() |
smooth : bool
Specifies whether the item is smoothed or not
Primarily used in image based items to decide if the item should use smooth sampling or not. Smooth sampling is performed using linear interpolation, while non-smooth is performed using nearest neighbor.
In Qt Quick 2.0, this property has minimal impact on performance.
By default, this property is set to true.
Access functions:
| bool | smooth() const |
| void | setSmooth(bool) |
Notifier signal:
| void | smoothChanged(bool) |
state : QString
This property holds the name of the current state of the item.
If the item is in its default state, that is, no explicit state has been set, then this property holds an empty string. Likewise, you can return an item to its default state by setting this property to an empty string.
Access functions:
| QString | state() const |
| void | setState(const QString &) |
Notifier signal:
| void | stateChanged(const QString &) |
See also Qt Quick States.
transformOrigin : TransformOrigin
This property holds the origin point around which scale and rotation transform.
Nine transform origins are available, as shown in the image below. The default transform origin is Item.Center.

Access functions:
| QQuickItem::TransformOrigin | transformOrigin() const |
| void | setTransformOrigin(QQuickItem::TransformOrigin) |
Notifier signal:
| void | transformOriginChanged(QQuickItem::TransformOrigin) |
visible : bool
This property holds whether the item is visible. By default this is true.
Setting this property directly affects the visible value of child items. When set to false, the visible values of all child items also become false. When set to true, the visible values of child items are returned to true, unless they have explicitly been set to false.
(Because of this flow-on behavior, using the visible property may not have the intended effect if a property binding should only respond to explicit property changes. In such cases it may be better to use the opacity property instead.)
If this property is set to false, the item will no longer receive mouse events, but will continue to receive key events and will retain the keyboard focus if it has been set. (In contrast, setting the enabled property to false stops both mouse and keyboard events, and also removes focus from the item.)
Note: This property's value is only affected by changes to this property or the parent's visible property. It does not change, for example, if this item moves off-screen, or if the opacity changes to 0.
Access functions:
| bool | isVisible() const |
| void | setVisible(bool) |
Notifier signal:
| void | visibleChanged() |
width : qreal
This property holds the width of this item.
Access functions:
| qreal | width() const |
| void | setWidth(qreal) |
| void | resetWidth() |
Notifier signal:
| void | widthChanged() |
x : qreal
Defines the item's x position relative to its parent.
Access functions:
| qreal | x() const |
| void | setX(qreal) |
Notifier signal:
| void | xChanged() |
y : qreal
Defines the item's y position relative to its parent.
Access functions:
| qreal | y() const |
| void | setY(qreal) |
Notifier signal:
| void | yChanged() |
z : qreal
Sets the stacking order of sibling items. By default the stacking order is 0.
Items with a higher stacking value are drawn on top of siblings with a lower stacking order. Items with the same stacking value are drawn bottom up in the order they appear. Items with a negative stacking value are drawn under their parent's content.
The following example shows the various effects of stacking order.
| Same z - later children above earlier children:Item { Rectangle { color: "red" width: 100; height: 100 } Rectangle { color: "blue" x: 50; y: 50; width: 100; height: 100 } } |
| Higher z on top:Item { Rectangle { z: 1 color: "red" width: 100; height: 100 } Rectangle { color: "blue" x: 50; y: 50; width: 100; height: 100 } } |
| Same z - children above parents:Item { Rectangle { color: "red" width: 100; height: 100 Rectangle { color: "blue" x: 50; y: 50; width: 100; height: 100 } } } |
| Lower z below:Item { Rectangle { color: "red" width: 100; height: 100 Rectangle { z: -1 color: "blue" x: 50; y: 50; width: 100; height: 100 } } } |
Access functions:
| qreal | z() const |
| void | setZ(qreal) |
Notifier signal:
| void | zChanged() |







