> 文章列表 > QML控件--Container

QML控件--Container

QML控件--Container

文章目录

  • 一、控件基本信息
  • 二、控件说明
  • 三、属性成员
  • 四、成员函数

一、控件基本信息

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.0ApplicationWindow{visible: true;  //ApplicationWindow默认不可见width: 1280;height: 720;Row {anchors.fill: parent;TabBar {id: tabBarcurrentIndex: 0width: parent.width - addButton.width - btnDelete.widthTabButton { text: "TabButton" }}Component { //组件id: tabButtonTabButton { text: "TabButton" }}Button {id: addButtontext: "+"flat: trueonClicked: {//Component::createObject(tabBar)可以创建Component组件对象tabBar.addItem(tabButton.createObject(tabBar))  //addItem向容器中增加子项console.log("added:", tabBar.itemAt(tabBar.count - 1))}}Button {id: btnDeletetext: "-"flat: trueonClicked: {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) 删除并返回索引处的项目,项目的所有权转移给调用者