文章目录
- 一、控件基本信息
- 二、控件说明
- 三、属性成员
- 四、成员函数
一、控件基本信息
Import Statement: import QtQuick.Controls 2.14
 Since: Qt 5.7
 Inherits: Control
 Inherited By: DialogButtonBox, MenuBar, SwipeView, and TabBar
二、控件说明
Container(容器):是类容器用户界面控件的基本类型,允许动态插入和删除项;
- 项被静态地声明为Container的子项,但也可以动态地添加、插入、移动和删除项;
- 容器中的项可以使用itemAt()或contentChildren来访问;
- 大多数容器都有“当前”项的概念,当前项是通过currentIndex属性指定的,并且可以使用只读的currentItem属性访问;
下面的示例演示了向TabBar动态插入项(TabBar是Container的子类之一)
 
import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.14
import QtQuick.Layouts 1.0
ApplicationWindow{
    visible: true;  //ApplicationWindow默认不可见
    width: 1280;
    height: 720;
    Row {
        anchors.fill: parent;
        TabBar {
            id: tabBar
            currentIndex: 0
            width: parent.width - addButton.width - btnDelete.width
            TabButton { text: "TabButton" }
        }
        Component { //组件
            id: tabButton
            TabButton { text: "TabButton" }
        }
        Button {
            id: addButton
            text: "+"
            flat: true
            onClicked: {
                //Component::createObject(tabBar)可以创建Component组件对象
                tabBar.addItem(tabButton.createObject(tabBar))  //addItem向容器中增加子项
                console.log("added:", tabBar.itemAt(tabBar.count - 1))
            }
        }
        Button {
            id: btnDelete
            text: "-"
            flat: true
            onClicked: {
                tabBar.removeItem(tabBar.itemAt(tabBar.count-1));   //removeItem移除容器中的子项,itemAt可以索引容器中的子项
            }
        }
    }
}
三、属性成员
| 属性 | 说明 | 
|---|---|
| contentChildren : list<Item> | 内容子项的列表, contentChildren不包含非可视 QML 对象,插入或移动项目时会重新排序 | 
| contentData : list<Object> | 内容数据列表, contentData确实包含非可视 QML 对象,插入或移动项目时不会重新排序 | 
| contentHeight : real | 内容尺寸,用于计算容器的总隐含尺寸 ,除非显式覆盖,否则内容尺寸会根据容器中项目的隐式尺寸自动计算 | 
| contentModel : model | 项目的内容模型,内容模型用于可视化项目 | 
| contentWidth : real | 内容尺寸,用于计算容器的总隐含尺寸,除非显式覆盖,否则内容尺寸会根据容器中项目的隐式尺寸自动计算 | 
| count : int | 项目的数量 | 
| currentIndex : int | 当前项目的索引 | 
| currentItem : Item | 当前项目 | 
四、成员函数
| 属性 | 说明 | 
|---|---|
| void addItem(item) | 添加一个项目 | 
| void decrementCurrentIndex() | 递减容器的当前索引 | 
| void incrementCurrentIndex() | 递增容器的当前索引 | 
| void insertItem(index, Item item) | 在 index 处插入一个项目 | 
| Item itemAt(index) | 返回 index 处的项目,如果不存在则返回 null | 
| void moveItem(from, int to) | 将 from 处的项目移动到 to | 
| void removeItem(item) | 移除并销毁指定的项目 | 
| void setCurrentIndex(index) | 设置容器的当前索引 | 
| void takeItem(index) | 删除并返回索引处的项目,项目的所有权转移给调用者 | 



















