Item QML Type▲
-
Import Statement: import QtQuick
-
Inherited By:: AnimatedSprite, BorderImage, Canvas, Column, ColumnLayout, DropArea, Flickable, Flipable, Flow, FocusScope, Grid, GridLayout, Image, Loader, MouseArea, MultiPointTouchArea, ParticlePainter, PathView, PinchArea, Rectangle, Repeater, Row, RowLayout, ShaderEffect, ShaderEffectSource, Shape, SpriteSequence, StackLayout, Text, TextEdit, and TextInput
-
Inherits: QtObject
-
Instantiates:: QQuickItem
-
Group: Item is part of qtquick-visual
Detailed Description▲
The Item type is the base type for all visual items in Qt Quick.
All visual items in Qt Quick inherit from Item. Although an Item object 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.
The Item type can be useful for grouping several items under a single root visual item. For example:
import
QtQuick 2.0
Item
{
Image
{
source
:
"tile.png"
}
Image
{
x
:
80
width
:
100
height
:
100
source
:
"tile.png"
}
Image
{
x
:
190
width
:
100
height
:
100
fillMode
:
Image.Tile
source
:
"tile.png"
}
}
Event Handling▲
All Item-based visual types can use Input Handlers to handle incoming input events (subclasses of QInputEvent), such as mouse, touch and key events. This is the preferred declarative way to handle events.
An alternative way to handle touch events is to subclass QQuickItem, call setAcceptTouchEvents() in the constructor, and override touchEvent(). Accept the entire event to stop delivery to items underneath, and to exclusively grab for all the event's touch points. Use QPointerEvent::setExclusiveGrabber() to grab only certain touchpoints, and allow the event to be delivered further.
Likewise, a QQuickItem subclass can call setAcceptedMouseButtons() to register to receive mouse button events, setAcceptHoverEvents() to receive hover events (mouse movements while no button is pressed), and override the virtual functions mousePressEvent(), mouseMoveEvent(), and mouseReleaseEvent(). Those can also accept the event to prevent further delivery and get an implicit grab at the same time; or explicitly grab the single QEventPoint that the QMouseEvent carries.
Key handling is available to all Item-based visual types via the Keys attached property. The Keys attached property provides basic signals such as pressed and released, as well as signals for specific keys, such as spacePressed. The example below assigns keyboard focus to the item and handles the left key via the general onPressed handler and the return key via the onReturnPressed handler:
import
QtQuick 2.0
Item
{
focus
:
true
Keys.onPressed
:
(event)=&
gt; {
if
(event.key ==
Qt.Key_Left) {
console.log
(
"move left"
);
event
.
accepted =
true;
}
}
Keys.onReturnPressed
:
console.log("Pressed return"
);
}
See the Keys attached property for detailed documentation.
Layout Mirroring▲
Item layouts can be mirrored using the LayoutMirroring attached property. This causes anchors to be horizontally reversed, and also causes items that lay out or position their children (such as ListView or Row) to horizontally reverse the direction of their layouts.
See LayoutMirroring for more details.
Item Layers▲
An Item will normally be rendered directly into the window it belongs to. However, by setting layer.enabled, it is possible to delegate the item and its entire subtree into an offscreen surface. Only the offscreen surface, a texture, will be then drawn into the window.
If it is desired to have a texture size different from that of the item, this is possible using layer.textureSize. To render only a section of the item into the texture, use layer.sourceRect. It is also possible to specify layer.sourceRect so it extends beyond the bounds of the item. In this case, the exterior will be padded with transparent pixels.
The item will use linear interpolation for scaling if layer.smooth is set to true and will use mipmap for downsampling if layer.mipmap is set to true. Mipmapping may improve visual quality of downscaled items. For mipmapping of single Image items, prefer Image::mipmap.
Layer Opacity vs Item Opacity▲
When applying opacity to an item hierarchy the opacity is applied to each item individually. This can lead to undesired visual results when the opacity is applied to a subtree. Consider the following example:
|
Non-layered Opacity Sélectionnez
|
A layer is rendered with the root item's opacity being 1, and then the root item's opacity is applied to the texture when it is drawn. This means that fading in a large item hierarchy from transparent to opaque, or vice versa, can be done without the overlap artifacts that the normal item by item alpha blending has. Here is the same example with layer enabled:
![]() |
Layered Opacity Sélectionnez
|
Combined with ShaderEffects▲
Setting layer.enabled to true will turn the item into a texture provider, making it possible to use the item directly as a texture, for instance in combination with the ShaderEffect type.
It is possible to apply an effect on a layer at runtime using layer.effect:
Item
{
id
:
layerRoot
layer.enabled
:
true
layer.effect
:
ShaderEffect
{
fragmentShader
:
"effect.frag.qsb"
}
}
See ShaderEffect for more information about using effects.
layer.enabled is actually just a more convenient way of using ShaderEffectSource.
Memory and Performance▲
When an item's layer is enabled, the scene graph will allocate memory in the GPU equal to width x height x 4. In memory constrained configurations, large layers should be used with care.
In the QPainter / QWidget world, it is sometimes favorable to cache complex content in a pixmap, image or texture. In Qt Quick, because of the techniques already applied by the scene graph renderer, this will in most cases not be the case. Excessive draw calls are already reduced because of batching and a cache will in most cases end up blending more pixels than the original content. The overhead of rendering to an offscreen and the blending involved with drawing the resulting texture is therefore often more costly than simply letting the item and its children be drawn normally.
Also, an item using a layer can not be batched during rendering. This means that a scene with many layered items may have performance problems.
Layering can be convenient and useful for visual effects, but should in most cases be enabled for the duration of the effect and disabled afterwards.
Property Documentation▲
children : list<Item>▲
resources : list<QtObject>
The children property contains the list of visual children of this item. The resources property contains non-visual resources that you want to reference by name.
It is not generally necessary to refer to these properties when adding child items or resources, as the default data property will automatically assign child objects to the children and resources properties as appropriate. See the data documentation for details.
height : real▲
width : real
x : real
y : real
Defines the item's position and size. The default value is 0.
The (x,y) position is relative to the parent.
Item
{
x
:
100
; y
:
100
; width
:
100
; height
:
100
}
implicitHeight : real▲
implicitWidth : real
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 : 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
{
id
:
focusScope
focus
:
true
TextInput
{
id
:
input
focus
:
true
}
}
}
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.
The tab focus chain traverses elements by first visiting the parent, and then its children in the order they occur in the children property. Pressing the tab key on an item in the tab focus chain will move keyboard focus to the next item in the chain. Pressing BackTab (normally Shift+Tab) will move focus to the previous item.
To set up a manual tab focus chain, see KeyNavigation. Tab key events used by Keys or KeyNavigation have precedence over focus chain behavior; ignore the events in other key handlers to allow it to propagate.
anchors group▲
anchors.alignWhenCentered : bool
anchors.baseline : AnchorLine
anchors.baselineOffset : real
anchors.bottom : AnchorLine
anchors.bottomMargin : real
anchors.centerIn : Item
anchors.fill : Item
anchors.horizontalCenter : AnchorLine
anchors.horizontalCenterOffset : real
anchors.left : AnchorLine
anchors.leftMargin : real
anchors.margins : real
anchors.right : AnchorLine
anchors.rightMargin : real
anchors.top : AnchorLine
anchors.topMargin : real
anchors.verticalCenter : AnchorLine
anchors.verticalCenterOffset : real
Anchors provide a way to position an item by specifying its relationship with other items.
Margins apply to top, bottom, left, right, and fill anchors. The anchors.margins property can be used to set all of the various margins at once, to the same value. It will not override a specific margin that has been previously set; to clear an explicit margin set its value to undefined. Note that margins are anchor-specific and are not applied if an item does not use anchors.
Offsets apply for horizontal center, vertical center, and baseline anchors.
![]() |
Text anchored to Image, horizontally centered and vertically below, with a margin. Sélectionnez
|
![]() |
Left of Text anchored to right of Image, with a margin. The y property of both defaults to 0. Sélectionnez
|
anchors.fill provides a convenient way for one item to have the same geometry as another item, and is equivalent to connecting all four directional anchors.
To clear an anchor value, set it to undefined.
anchors.alignWhenCentered (default true) forces centered anchors to align to a whole pixel; if the item being centered has an odd width or height, the item will be positioned on a whole pixel rather than being placed on a half-pixel. This ensures the item is painted crisply. There are cases where this is not desirable, for example when rotating the item jitters may be apparent as the center is rounded.
You can only anchor an item to siblings or a parent.
For more information see Anchor Layouts.
antialiasing : bool▲
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.
baselineOffset : int▲
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.
childrenRect group▲
[read-only] childrenRect.height : real
[read-only] childrenRect.width : real
[read-only] childrenRect.x : real
[read-only] childrenRect.y : real
This read-only 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
}
}
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.
Clipping can affect rendering performance. See Clipping for more information.
[since 5.11] containmentMask : QObject*▲
This property holds an optional mask for the Item to be used in the QtQuick::Item::contains() method. Its main use is currently to determine whether a pointer event has landed into the item or not.
By default the contains method will return true for any point within the Item's bounding box. containmentMask allows for more fine-grained control. For example, if a custom C++ QQuickItem subclass with a specialized contains() method is used as containmentMask:
Item {
id: item; containmentMask: AnotherItem {
id: anotherItem }
}
item's contains method would then return true only if anotherItem's contains() implementation returns true.
A Shape can be used as a mask, to make an item react to pointer events only within a non-rectangular region:
![]() |
Sélectionnez
|
It is also possible to define the contains method in QML. For example, to create a circular item that only responds to events within its actual bounds:
![]() |
Sélectionnez
|
This property was introduced in Qt 5.11.
See Also▲
See also Qt Quick Examples - Shapes
[default] data : list<QtObject>▲
The data property allows you to freely mix visual children and resources in an item. If you assign a visual item to the data list it becomes a child and if you assign any other object type, it is added as a resource.
So you can write:
Item
{
Text
{}
Rectangle
{}
Timer
{}
}
instead of:
Item
{
children
:
[
Text
{}
,
Rectangle
{}
]
resources
:
[
Timer
{}
]
}
It should not generally be necessary to refer to the data property, as it is the default property for Item and thus all child items are automatically assigned to this property.
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 no longer receive keyboard events.
See Also▲
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
}
}
See Also▲
See also activeFocus, Keyboard Focus in Qt Quick
layer.effect : Component▲
Holds the effect that is applied to this layer.
The effect is typically a ShaderEffect component, although any Item component can be assigned. The effect should have a source texture property with a name matching layer.samplerName.
See Also▲
See also layer.samplerName, Item Layers
layer.enabled : bool▲
Holds whether the item is layered or not. Layering is disabled by default.
A layered item is rendered into an offscreen surface and cached until it is changed. Enabling layering for complex QML item hierarchies can sometimes be an optimization.
None of the other layer properties have any effect when the layer is disabled.
See Also▲
See also Item Layers
layer.format : enumeration▲
This property defines the internal format of the texture. Modifying this property makes most sense when the layer.effect is also specified. Depending on the OpenGL implementation, this property might allow you to save some texture memory.
-
ShaderEffectSource.Alpha - GL_ALPHA;
-
ShaderEffectSource.RGB - GL_RGB
-
ShaderEffectSource.RGBA - GL_RGBA
ShaderEffectSource.RGB and ShaderEffectSource.Alpha should be used with caution, as support for these formats in the underlying hardware and driver is often not present.
See Also▲
See also Item Layers
layer.mipmap : bool▲
If this property is true, mipmaps are generated for the texture.
Some OpenGL ES 2 implementations do not support mipmapping of non-power-of-two textures.
See Also▲
See also Item Layers
layer.samplerName : string▲
Holds the name of the effect's source texture property.
This value must match the name of the effect's source texture property so that the Item can pass the layer's offscreen surface to the effect correctly.
See Also▲
See also layer.effect, ShaderEffect, Item Layers
[since 5.10] layer.samples : enumeration▲
This property allows requesting multisampled rendering in the layer.
By default multisampling is enabled whenever multisampling is enabled for the entire window, assuming the scenegraph renderer in use and the underlying graphics API supports this.
By setting the value to 2, 4, etc. multisampled rendering can be requested for a part of the scene without enabling multisampling for the entire scene. This way multisampling is applied only to a given subtree, which can lead to significant performance gains since multisampling is not applied to other parts of the scene.
Enabling multisampling can be potentially expensive regardless of the layer's size, as it incurs a hardware and driver dependent performance and memory cost.
This property is only functional when support for multisample renderbuffers and framebuffer blits is available. Otherwise the value is silently ignored.
This property was introduced in Qt 5.10.
layer.smooth : bool▲
Holds whether the layer is smoothly transformed. When enabled, sampling the layer's texture is performed using linear interpolation, while non-smooth results in using the nearest filtering mode.
By default, this property is set to false.
See Also▲
See also Item Layers
layer.sourceRect : rect▲
This property defines the rectangular area of the item that should be rendered into the texture. The source rectangle can be larger than the item itself. If the rectangle is null, which is the default, then the whole item is rendered to the texture.
See Also▲
See also Item Layers
[since 5.6] layer.textureMirroring : enumeration▲
This property defines how the generated texture should be mirrored. The default value is ShaderEffectSource.MirrorVertically. Custom mirroring can be useful if the generated texture is directly accessed by custom shaders, such as those specified by ShaderEffect. If no effect is specified for the layered item, mirroring has no effect on the UI representation of the item.
-
ShaderEffectSource.NoMirroring - No mirroring
-
ShaderEffectSource.MirrorHorizontally - The generated texture is flipped along X-axis.
-
ShaderEffectSource.MirrorVertically - The generated texture is flipped along Y-axis.
This property was introduced in Qt 5.6.
layer.textureSize : size▲
This property holds the requested pixel size of the layers texture. If it is empty, which is the default, the size of the item is used.
Some platforms have a limit on how small framebuffer objects can be, which means the actual texture size might be larger than the requested size.
See Also▲
See also Item Layers
layer.wrapMode : enumeration▲
This property defines the wrap modes associated with the texture. Modifying this property makes most sense when the layer.effect is specified.
-
ShaderEffectSource.ClampToEdge - GL_CLAMP_TO_EDGE both horizontally and vertically
-
ShaderEffectSource.RepeatHorizontally - GL_REPEAT horizontally, GL_CLAMP_TO_EDGE vertically
-
ShaderEffectSource.RepeatVertically - GL_CLAMP_TO_EDGE horizontally, GL_REPEAT vertically
-
ShaderEffectSource.Repeat - GL_REPEAT both horizontally and vertically
Some OpenGL ES 2 implementations do not support the GL_REPEAT wrap mode with non-power-of-two textures.
See Also▲
See also Item Layers
opacity : real▲
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