QQuickItem Class▲
-
Header: QQuickItem
-
CMake:
find_package(Qt6 REQUIRED COMPONENTS Quick)
target_link_libraries(mytarget PRIVATE Qt6::Quick)
-
qmake: QT += quick
-
Inherited By: QQuickFramebufferObject and QQuickPaintedItem
-
Instantiated By: qml-qtquick-item.xml
-
Inherits: QObject and QQmlParserStatus
-
Inherited By: QQuickFramebufferObject and QQuickPaintedItem
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 accelerated graphics APIs, such as OpenGL, Vulkan, Metal, or Direct 3D. 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.
It is crucial that graphics 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.
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, depending on the platform and QQuickWindow configuration, when the window is hidden using QQuickWindow::hide(), or when it is closed. If the item class implements a slot named invalidateSceneGraph(), this slot will be called on the rendering thread while the GUI thread is blocked. This is equivalent to connecting to QQuickWindow::sceneGraphInvalidated(). When rendering through OpenGL, 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 through EGL_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 null or 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.
Use of QObject::deleteLater() to clean up graphics resources is strongly discouraged as this will make the delete operation 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.
Using QQuickPaintedItem uses an indirect 2D surface to render its content, using software rasterization, 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▲
See also QQuickWindow, QQuickPaintedItem
Member Type Documentation▲
enum QQuickItem::Flag▲
flags QQuickItem::Flags
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. |
QQuickItem::ItemIsViewport |
0x20 |
Indicates that the item defines a viewport for its children. |
QQuickItem::ItemObservesViewport |
0x40 |
Indicates that the item wishes to know the viewport bounds when any ancestor has the ItemIsViewport flag set. |
The Flags type is a typedef for QFlags<Flag>. It stores an OR combination of Flag values.
See Also▲
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▲
See also transformOrigin(), setTransformOrigin()
Property Documentation▲
implicitHeight : qreal▲
implicitWidth : qreal
Defines the preferred width or height of the Item.
If width or height is not specified, an item's effective size will be determined by its implicitWidth or implicitHeight.
However, if an item is the child of a layout, the layout will determine the item's preferred size using its implicit size. In such a scenario, the explicit width or height will be ignored.
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
}
}
Using implicitWidth of Text or TextEdit and setting the width explicitly incurs a performance penalty as the text must be laid out twice.
[read-only] 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▲
See also focus, 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)
[read-only] 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:
-
childrenRect()
Notifier signal:
-
void childrenRectChanged(const QRectF &)
clip : bool▲
This property holds whether cli