Container 是
类容器用户界面控件
的基本类型,允许动态插入和删除项目。是
DialogButtonBox
、
MenuBar
、
SplitView
、
SwipeView
、
TabBar
的基类。
1.1、使用容器
通常,项目被静态声明为 Container 的子项,但也可以动态添加、插入、移动和删除项目。可以使用
itemAt
() 或
contentChildren
访问容器中的项目。
大多数容器都有“当前项”的概念。当前项是通过
currentIndex
属性指定的,并且可以使用只读的
currentItem
属性进行访问。
以下示例将项目动态插入到
TabBar
:
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))
1.2、管理当前索引
当同时使用多个容器时,例如 TabBar 和 SwipeView,它们的 currentIndex 属性可以相互绑定以保持同步。 当用户与任一容器交互时,其当前索引更改会自动传播到另一个容器。
但是在 JavaScript 中分配 currentIndex 值会删除相应的绑定。为了保留绑定,应使用以下方法更改当前索引:
- 增量当前索引:incrementCurrentIndex()
- 递减当前索引:decrementCurrentIndex()
- 设置当前索引:setCurrentIndex()
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
二、属性成员
1、contentChildren : list<Item>
内容子项的列表。该列表包含在 QML 中声明为容器子项的所有项目,以及分别使用 addItem() 和 insertItem() 方法动态添加或插入的项目。
与 contentData 不同,contentChildren 不包含非可视 QML 对象。插入或移动项目时会重新排序。
2、[default] contentData : list<Object>
内容数据列表。该列表包含在 QML 中声明为容器子项的所有对象,以及分别使用 addItem() 和 insertItem() 方法动态添加或插入的项目。
与 contentChildren 不同,contentData 确实包含非可视 QML 对象。 插入或移动项目时不会重新排序。
3、contentHeight : real / contentWidth : real
内容尺寸。用于计算容器的总隐含尺寸。
除非显式覆盖,否则内容尺寸会根据容器中项目的隐式尺寸自动计算。
4、[read-only] contentModel : model
项目的内容模型。内容模型用于可视化项目。
Container {
id: container
contentItem: ListView {
model: container.contentModel
5、[read-only] count : int
项目的数量。
6、currentIndex : int
当前项目的索引。
7、[read-only] currentItem : Item
当前项目。
三、成员函数
1、void addItem(Item item)
添加一个项目。
2、void decrementCurrentIndex()
void incrementCurrentIndex()
void setCurrentIndex(int index)
递减 / 递增 / 设置容器的当前索引。
可以调用这些方法来更改当前索引,而不会破坏现有的 currentIndex 绑定。
3、void insertItem(int index, Item item)
在 index 处插入一个项目。
4、Item itemAt(int index)
返回 index 处的项目,如果不存在则返回 null。
5、void moveItem(int from, int to)
将 from 处的项目移动到 to。
6、void removeItem(Item item)
移除并销毁指定的项目。
7、Item takeItem(int index)
删除并返回索引处的项目。项目的所有权转移给调用者。
源码:https://github.com/sueRimn/QML-ExampleDemos
想实现垂直竖直方向的TabView,查看文档,并没有对此的属性说明,所以跳出局限,自己做一个实例,录制软件没有录入鼠标
效果如下:
核心代码如下:
Column{
id:coloumn;
spacing: 0...
在实现自定义容器时,API中最重要的部分是contentModel,它提供了包含的项目,可以用作项目视图和重复器的委托模型。TabBar其实就是选项卡,TabBar是由TabButton控件填充,TabBar可以与任何提供currentIndex属性的布局或容器控件一起使用,如StackLayout或SwipeView。使用也是很简单的,其实就利用上面的框架,我们可以选择性的显示 menuBar、header、footer、或者内容区域的。如果不需要使用对应的控件部分的话,就可以不用写这部分内容的。
Import Statement:import QtQuick.Controls 2.14
Since:Qt 5.7
Inherits:ContainerSwipeView是一个带滑动功能的QStackedWidget
TabBar + SwipeView 使用
我们实现的核心容器组件命名为 VideoContainer.qml
当然,需要为动态创建的实例区分类型, 这里使用了 Qml 的新特性 enum
enum VideoType {
QT QML C++扩展开发视频课程免费QT视频课程 您可以看免费1000+个QT技术视频
免费QT视频课程 QT统计图和QT数据可视化视频免费看
免费QT视频课程 QT性能优化视频免费看
免费QT视频课程 QT界面美化视频免费看QT6_QML布局容器概述
QT6 QML布局容器概述
QT6 QML是Qt框架的最新版本,它提供了一套丰富的布局容器,用于在用户界面中排列和定位元素。这些布局容