Container QML Type
提供容器通用功能的抽象基础类型。更多...
| Import Statement: | import QtQuick.Controls |
| Inherits: | |
| Inherited By: | DialogButtonBox, MenuBar, SplitView, SwipeView, and TabBar |
属性
- contentChildren : list<Item>
- contentData : list<QtObject>
- contentHeight : real
(since QtQuick.Controls 2.5 (Qt 5.12)) - contentModel : model
- contentWidth : real
(since QtQuick.Controls 2.5 (Qt 5.12)) - count : int
- currentIndex : int
- currentItem : Item
方法
- void addItem(Item item)
- void decrementCurrentIndex()
(since QtQuick.Controls 2.1 (Qt 5.8)) - void incrementCurrentIndex()
(since QtQuick.Controls 2.1 (Qt 5.8)) - void insertItem(int index, Item item)
- Item itemAt(int index)
- void moveItem(int from, int to)
- void removeItem(Item item)
(since QtQuick.Controls 2.3 (Qt 5.10)) - void setCurrentIndex(int index)
- Item takeItem(int index)
(since QtQuick.Controls 2.3 (Qt 5.10))
详细说明
Container 是容器类用户界面控件的基础类型,支持动态插入和移除项目。
使用容器
通常,项目会作为 Container 的子元素静态声明,但也可以通过add 、insert 、move 和remove 动态添加项目。可以使用itemAt() 或contentChildren 访问容器中的项目。
大多数容器都具有“当前”项的概念。当前项通过currentIndex 属性指定,并可通过只读属性currentItem 进行访问。
以下示例演示了如何向TabBar (作为 Container 的具体实现之一)动态插入项目。
Row {
TabBar {
id: tabBar
currentIndex: 0
width: parent.width - addButton.width
TabButton { text: "TabButton" }
}
Component {
id: tabButton
TabButton { text: "TabButton" }
}
Button {
id: addButton
text: "+"
flat: true
onClicked: {
tabBar.addItem(tabButton.createObject(tabBar))
console.log("added:", tabBar.itemAt(tabBar.count - 1))
}
}
}管理当前索引
当同时使用多个容器(如TabBar 和SwipeView )时,可以将它们的currentIndex 属性相互绑定,以保持同步。当用户与其中任一容器交互时,其当前索引的变化会自动传播到另一个容器。
但请注意,在 JavaScript 中为currentIndex 赋值会解除相应的绑定。若要保留绑定关系,请使用以下方法来更改当前索引:
TabBar {
id: tabBar
currentIndex: swipeView.currentIndex
}
SwipeView {
id: swipeView
currentIndex: tabBar.currentIndex
}
Button {
text: qsTr("Home")
onClicked: swipeView.setCurrentIndex(0)
enabled: swipeView.currentIndex != 0
}
Button {
text: qsTr("Previous")
onClicked: swipeView.decrementCurrentIndex()
enabled: swipeView.currentIndex > 0
}
Button {
text: qsTr("Next")
onClicked: swipeView.incrementCurrentIndex()
enabled: swipeView.currentIndex < swipeView.count - 1
}实现容器
Container 类不提供任何默认的可视化效果。它用于实现诸如SwipeView 和TabBar 之类的容器。在实现自定义容器时,API 中最重要的部分是contentModel 方法,它以一种可供项视图和重复器作为对象模型使用的方式提供被包含的项。
Container {
id: container
contentItem: ListView {
model: container.contentModel
snapMode: ListView.SnapOneItem
orientation: ListView.Horizontal
}
Text {
text: "Page 1"
width: container.width
height: container.height
}
Text {
text: "Page 2"
width: container.width
height: container.height
}
}请注意,页面项的大小是手动设置的。这是因为该示例使用的是普通的 Container,它对视觉布局不作任何假设。在具体的 Container 实现(如SwipeView 和TabBar )中,通常无需为项指定大小。
另请参阅 “容器控件”。
属性文档
contentChildren : list<Item>
该属性保存内容子节点的列表。
该列表包含所有在 QML 中被声明为容器子节点的项目,以及分别通过addItem() 和insertItem() 方法动态添加或插入的项目。
注意:与 ` contentData`不同, `contentChildren ` 不包含非可视化 QML 对象。当项目被插入或移动时,该列表会重新排序。
另请参阅 Item::children 和contentData 。
contentData : list<QtObject> [default]
该属性保存内容数据的列表。
该列表包含所有在 QML 中被声明为容器子对象的对象,以及分别通过addItem() 和insertItem() 方法动态添加或插入的项目。
注意:与 ` contentChildren`不同, `contentData ` 确实包含非可视化 QML 对象。当项目被插入或移动时,该列表不会重新排序。
另请参阅 Item::data 和contentChildren 。
contentHeight : real [since QtQuick.Controls 2.5 (Qt 5.12)]
该属性存储内容高度,用于计算容器的总隐式高度。
除非被显式覆盖,否则内容高度会根据容器中各项的隐式高度自动计算。
该属性在 QtQuick.Controls 2.5(Qt 5.12)中引入。
另请参阅 contentWidth 。
contentModel : model [read-only]
该属性保存了项目的内容模型。
内容模型用于可视化目的。它可作为模型分配给一个内容项,以呈现容器中的内容。
另请参阅 contentData 和contentChildren 。
contentWidth : real [since QtQuick.Controls 2.5 (Qt 5.12)]
该属性存储内容的宽度。它用于计算容器的总隐式宽度。
除非被显式覆盖,否则内容宽度会根据容器中各项的隐式宽度自动计算得出。
该属性于 QtQuick.Controls 2.5(Qt 5.12)中引入。
另请参阅 contentHeight 。
count : int [read-only]
该属性存储项目数量。
currentIndex : int
该属性保存当前项的索引。
另请参阅 currentItem 和Managing the Current Index 。
currentItem : Item [read-only]
该属性存储当前项目。
另请参阅 currentIndex 。
方法文档
void addItem(Item item)
添加一个item 。
[since QtQuick.Controls 2.1 (Qt 5.8)] void decrementCurrentIndex()
将容器的当前索引减1。
调用此方法可更改当前索引,同时不会破坏现有的currentIndex 绑定。
该方法于 QtQuick.Controls 2.1(Qt 5.8)中引入。
另请参阅 currentIndex 和Managing the Current Index 。
[since QtQuick.Controls 2.1 (Qt 5.8)] void incrementCurrentIndex()
将容器的当前索引递增。
调用此方法可修改当前索引,同时不会破坏现有的currentIndex 绑定。
该方法于 QtQuick.Controls 2.1(Qt 5.8)中引入。
另请参阅 currentIndex 和Managing the Current Index 。
void insertItem(int index, Item item)
在index 处插入一个item 。
Item itemAt(int index)
返回位于index 的项目;如果该项目不存在,则返回null 。
void moveItem(int from, int to)
将一个项目from 移动一个索引to 另一个索引。
[since QtQuick.Controls 2.3 (Qt 5.10)] void removeItem(Item item)
移除并销毁指定的item 。
该方法于 QtQuick.Controls 2.3(Qt 5.10)中引入。
void setCurrentIndex(int index)
设置容器的当前index 。
调用此方法可设置特定的当前索引,同时不会破坏现有的currentIndex 绑定。
另请参阅 currentIndex 和Managing the Current Index 。
[since QtQuick.Controls 2.3 (Qt 5.10)] Item takeItem(int index)
移除并返回位于index 处的项。
注意: 该项的所有 权将转移给调用方。
该方法于 QtQuick.Controls 2.3(Qt 5.10)中引入。
© 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.