本页内容

StackLayout QML Type

StackLayout 类提供了一个项目堆栈,其中每次仅显示一个项目。更多内容...

Import Statement: import QtQuick.Layouts
Inherits:

Item

属性

关联属性

  • index : int (since QtQuick.Layouts 1.15)
  • isCurrentItem : bool (since QtQuick.Layouts 1.15)
  • layout : StackLayout (since QtQuick.Layouts 1.15)

详细说明

为了更高效地使用此类型,建议您先了解Qt Quick Layouts 模块的一般工作原理。更多信息请参阅《Qt Quick Layouts 概述》。

可以通过设置 `currentIndex ` 属性来修改当前可见的项目。该索引对应于 StackLayout 子节点的顺序。

与大多数其他布局不同,子项的Layout.fillWidth 和Layout.fillHeight 属性默认设置为true 。因此,只要子项的Layout.maximumWidth 或Layout.maximumHeight 属性未限制,子项默认会填充以匹配StackLayout的大小。

通过将项目重新归属到布局中,即可将项目添加到布局中。同样地,通过将项目从布局中移除,即可将其从布局中删除。这两项操作都会影响布局的count 属性。

以下代码将创建一个 StackLayout,其中仅“plum”矩形可见。

StackLayout {
    id: layout
    anchors.fill: parent
    currentIndex: 1
    Rectangle {
        color: 'teal'
        implicitWidth: 200
        implicitHeight: 200
    }
    Rectangle {
        color: 'plum'
        implicitWidth: 300
        implicitHeight: 200
    }
}

StackLayout 中的项目支持以下附加属性:

点击此处了解有关附加属性的更多信息。

另请参阅 ColumnLayout 、GridLayout 、RowLayout 、StackView 以及Qt Quick Layouts 概述。

属性文档

count : int [read-only]

该属性存储属于该布局的项目数量。

只有作为StackLayout 子节点的项目才会成为布局的候选对象。

currentIndex : int

该属性保存当前在StackLayout 中可见的子项的索引。对于空布局,默认值为-1 ;否则,默认值为0 (指第一个项)。

自 6.5 版本起,在索引小于或等于当前索引的位置插入/移除新项时,当前索引会相应递增/递减,但会保留当前项。

附加属性文档

StackLayout.index : int [read-only attached, since QtQuick.Layouts 1.15]

该附加属性存储了StackLayout 中每个子项的索引。

该属性在 QtQuick.Layouts 1.15 版本中引入。

另请参阅 isCurrentItem 和layout 。

StackLayout.isCurrentItem : bool [read-only attached, since QtQuick.Layouts 1.15]

如果该子项是StackLayout 中的当前项,则此关联属性为true 。

该属性于 QtQuick.Layouts 1.15 版本中引入。

另请参阅 index 和layout 。

StackLayout.layout : StackLayout [read-only attached, since QtQuick.Layouts 1.15]

此附加属性保存了管理该子项的StackLayout 。

该属性在 QtQuick.Layouts 1.15 版本中引入。

另请参阅 index 和isCurrentItem 。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.