QML视图(ListView)
创始人
2024-06-02 10:32:06
0

ListView(列表视图)

ListView显示从内置 QML 类型(如 ListModel 和 XmlListModml)创建的模型的数据,或者在从 QAbstractItemModel 或 QAbstractListModel 继承的C++中定义的自定义模型类。

  • ListView继承自flickable,所以具有弹动效果
  • ListView按照水平或垂直布局 

常用的属性:

cacheBuffer确定是否将委托保留在视图的可见区域之外.大于零,则视图可以在指定的缓冲区内保持尽可能多的委托实例化
count返回项数

currentIndex

currentItem

currentSection

当前的索引

当前的项目

当前的部分:保存当前位于视图开头的部分

delegate设置委托
displaced设置通用过渡

displayMarginBeginning 

displayMarginEnd

允许在视图几何图形之外显示委托,默认值为 0
footer页脚

header

headerItem

标题

标题项

highlight 

highlightFollowsCurrentItem 

highlightItem 

(第二项为true才能使用)

highlightMoveDuration

highlightMoveVelocity

highlightResizeDuration

highlightResizeVelocity

需要突出的控件

保存突出显示是否由视图管理(默认为true)

保存从突出显示组件创建的突出显示项

高亮移动持续时间

高亮移动速度(默认值为 400 像素/秒)

高亮显示调整大小持续时间

高亮显示大小速度

keyNavigationEnabled

keyNavigationWraps 

保存列表的键导航是否已启用

保存列表是否换行键导航

model 保存为列表提供数据的模型

move

moveDisplaced

保存过渡以应用于视图中由于视图模型中的移动操作而移动的项

保存过渡以应用于视图模型中的移动操作所取代的项

populate保存要应用于最初为视图创建的项的过渡

remove

removeDisplaced

保存要应用于从视图中删除的项的过渡

保存转换以应用于视图中因删除视图中的其他项而替换的项

add

addDisplaced

保存要应用于从视图中添加的项的过渡

保存转换以应用于视图中因添加视图中的其他项而替换的项

spacing间距

附加属性:

ListView.delayRemove 此附加属性保存是否可以销毁委托(默认为true)
ListView.isCurrentItem

如果此委托是当前项,则此附加属性为 true

用于调整当前项的外观

ListView.nextSection属性保存下一个元素的部分
ListView.previousSection属性保存前一个元素的部分
ListView.section保存此元素的部分
ListView.view保存管理此委托实例的视图

 信号:

add()将项目添加到视图后,将立即发出此附加信号
remove()从视图中移除项目之前立即发出

方法:

positionViewAtBeginning()

positionViewAtEnd()

把位置定位在开头

把位置定位在结尾

decrementCurrentIndex()

incrementCurrentIndex()

递减当前索引

递增当前索引

forceLayout()此方法强制 ListView 立即响应模型中的任何未完成更改。

indexAt

itemAt

返回该位置的索引

返回该位置的项

positionViewAtIndex定位视图

以下为以上属性和方法的使用:

1.最常见的使用:

//创建模型
ListModel {  id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}
}//使用视图显示
ListView {width: 180; height: 200model: model1 //设置模型delegate: Text {    //使用Text显示数据text: name + ": " + number}
}

2.使用Component来进行委托

ListModel {id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}}Component{id:component1Row{spacing: 10Text {text:"姓名:"+name }Text {text:"电话:"+number}}}ListView{anchors.fill: parentmodel: model1  //设置模型delegate:component1 //委托}

3.方向和布局方向:

orientation(方向)

ListView.Vertical (default)竖直(默认)
ListView.Horizontal水平

layoutDirection(水平的布局方向)

Qt.LeftToRight (default) 从左到右
Qt.RightToLeft从右到左

 verticalLayoutDirection (竖直的布局方向)

ListView.TopToBottom (default) 从上到下
ListView.BottomToTop 从下到上

使用水平布局方向,从左到右:

 ListView{anchors.fill: parentmodel: model1delegate:component1orientation: ListView.Horizontal//设置水平布局,从左到右}

 使用水平布局方向,从右到左:

ListView{anchors.fill: parentmodel: model1delegate:component1orientation: ListView.Horizontal//设置水平布局layoutDirection: Qt.RightToLeft//从右到左布局}

 

 3.突出(高亮)的使用

ListModel {id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}}Rectangle {x:50;width: 200;height: 500Component {id: contactDelegateItem {width: 180; height: 40Column {Text { text: 'Name: ' + name }Text { text: 'Number: ' + number }}}}ListView {anchors.fill: parentmodel: model1 //设置模型delegate: contactDelegate //设置委托highlight: Rectangle { color: "lightsteelblue";} //设置高亮focus: true //获取焦点,使用键盘切换}}

添加弹动效果:

ListModel {id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}}Component {id: highlightRectangle {width: 180; height: 40color: "lightsteelblue"; radius: 5y: list.currentItem.yBehavior on y {SpringAnimation {spring: 3damping: 0.2}}}}Component{id:component1Column{spacing: 10Text {text:"姓名:"+name }Text {text:"电话:"+number}}}ListView {id: listwidth: 180; height: 200model: model1delegate: component1highlight: highlighthighlightFollowsCurrentItem: falsefocus: true}

4.委托中使用视图属性位置

委托会根据需要实例化,并且可以随时销毁。因此,状态永远不应存储在委托中

使用的方法:

      ListView {id:list1anchors.fill: parentmodel: model1Component {id: contactDelegateItem {width: list1.width //方法一height: list1.view.height //方法二Column {Text { text: 'Name: ' + name }Text { text: 'Number: ' + number }}}}delegate: contactDelegatehighlight: Rectangle { color: "lightsteelblue";}focus: true}

 ListView.isCurrentItem的使用

用于设置内容的属性,判断是否为当前项。

ListModel {id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}}ListView {width: 180; height: 200Component {id: contactsDelegateRectangle {id: wrapperwidth: 180height: contactInfo.height//当前项的话,背景颜色为黑色,不是的话为红色color: ListView.isCurrentItem ? "black" : "red"Text {id: contactInfotext: name + ": " + number//当前项的话,字体为红色,不是的话为黑色color: wrapper.ListView.isCurrentItem ? "red" : "black"}}}model: model1delegate: contactsDelegatefocus: true}

 5.标头和页脚和堆叠顺序

headerPositioning、footerPositioning (标头和页脚的定位)

ListView.InlineHeader(默认)页眉位于内容的开头,并像普通项目一样与内容一起移动。
ListView.OverlayHeader页眉位于视图的开头
ListView.PullBackHeader页眉位于视图的开头。标头可以通过向前移动内容来推开,并通过向后移动内容来拉回。

视图中的堆叠顺序:

属性z值
delegate1
footer1
header1
highlight0
section.deleate2

例子:

ListModel {id:model1ListElement { name: "Bill Smith";number: "555 3264"}ListElement { name: "John Brown";number: "555 8426"}ListElement { name: "Sam Wise";number: "555 0473"}}Component{id:component1Row{spacing: 10Text {text:"姓名:"+name }Text {text:"电话:"+number}}}ListView{id:list1anchors.fill: parentheader: Component{ //添加标题Rectangle{width: list1.widthcolor: "red"height: 30Text { text:"标题"}}}footer: Component{ //添加页脚Rectangle{width: list1.widthcolor: "red"height: 30Text { text:"页脚"}}}model: model1  //设置模型delegate:component1 //委托}

 6.listView中的鼠标事件

鼠标点击项,变为当前项:

currentIndex当前项
indexAt(x,y)获取x,y位置的索引
       ListView{id:list1anchors.fill: parentmodel: model1delegate:component1spacing: 10highlight: Rectangle{color: "lightsteelblue"}focus: trueMouseArea{  //设置鼠标区域id:mouse1anchors.fill: parentonPressed: {//获取鼠标位置的项,并作为当前项list1.currentIndex=list1.indexAt(mouse1.mouseX,mouse1.mouseY)}}}

7.对列表视图进行分组:

section group(组)

section.criteria (分组的标准)

ViewSection.FullString(默认)根据值创建部分
ViewSection.FirstCharacter根据值的第一个字符创建部分

section.delegate (分组的代理)

section.property  (分组的属性)

section.labelPositioning(标签定位)

ViewSection.InlineLabels部分标签在分隔部分的项目委托之间内联显示(默认)
ViewSection.CurrentLabelAtStart 当前分区标签在移动视图时会粘附在视图的开头
ViewSection.NextLabelAtEnd下一个部分标签(超出所有可见部分)在移动视图时粘在视图的末尾

注意: 向 ListView 添加节不会自动按节条件对列表项重新排序。如果模型不是按部分排序的,则创建的部分可能不是唯一的;不同部分之间的每个边界都将导致创建部分标题,即使该部分存在于其他地方也是如此。

例子:

ListModel {id: nameModelListElement { name: "Alice"; team: "Crypto" }ListElement { name: "Bob"; team: "Crypto" }ListElement { name: "Jane"; team: "QA" }ListElement { name: "Victor"; team: "QA" }ListElement { name: "Wendy"; team: "Graphics" }}Component {id: nameDelegateText {text: name;font.pixelSize: 24anchors.left: parent.leftanchors.leftMargin: 2}}ListView {anchors.fill: parentmodel: nameModeldelegate: nameDelegatefocus: truehighlight: Rectangle {color: "lightblue"width: parent.width}section {   //添加分组property: "team" //设置分组的属性criteria: ViewSection.FullString //设置分组标准delegate: Rectangle {  //设置代理color: "#b0dfb0"width: parent.widthheight: childrenRect.height + 4Text {anchors.horizontalCenter: parent.horizontalCenterfont.pixelSize: 16font.bold: truetext: section  //内容为分组的内容}}}}

参考文档:

ListView QML Type | Qt Quick 5.15.12

Qt Quick Examples - Views | Qt Quick 5.15.12

相关内容

热门资讯

监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...
MySQL下载和安装(Wind... 前言:刚换了一台电脑,里面所有东西都需要重新配置,习惯了所...
MFC文件操作  MFC提供了一个文件操作的基类CFile,这个类提供了一个没有缓存的二进制格式的磁盘...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
有效的括号 一、题目 给定一个只包括 '(',')','{','}'...
【Ctfer训练计划】——(三... 作者名:Demo不是emo  主页面链接:主页传送门 创作初心ÿ...