当前位置:网站首页>Roson的Qt之旅#99 QML表格控件-TableView
Roson的Qt之旅#99 QML表格控件-TableView
2022-07-25 16:35:00 【Allen Roson】
TableView类似于QML中的ListView,类似Widget中的QTableView,它增加了滚动条、选择和可调整大小的标题部分。与ListView一样,每一行的数据都是通过一个模型提供的:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel{
id:listModel
ListElement{title:"A";author:"Xiaoming"}
ListElement{title:"B";author:"Liling"}
ListElement{title:"C";author:"ABing"}
}
TableView{
anchors.fill: parent
model: listModel
TableViewColumn{
role:"title"
title: "标题"
width: 100
}
TableViewColumn{
role:"author"
title: "作者"
width: 200
}
}
}

上面的代码使用了ListModel作为数据模型来提供数据,其中每一行数据使用一个ListElement对象表示,这里指定了title和author两个字段。要想使用模型中的数据,在TableView 中需要使用 TableViewColumn来指定模型中的字段,每一个TableViewColumn对应一个字段,其中需要指定role即模型中字段名称,而title是要显示的每一列的列头。TableViewColumn类型中还包含了visible、width resizable、mova-ble,delegate等属性,其中delegate属性可以给特定的列使用委托来指定显示方式,在委托中可以访问特定的属性。
TableView可以通过itemDelegate属性,使用委托来绘制指定单元格。它与TableViewColumn的delegate效果相同。另外还可以通过rowDelegate和 headerDelegate 来使用委托绘制行或列头。TableView类型还提供了一些常用的函数,比如ad-dColumn() ,getColumn() 、 insertColumn() 、 removeColumn()、rowAt(x,y)等,进行相关操作。另外,当行被点击后会发射clicked()或doubleClick()信号。
TableView视图本身不提供排序。这必须在模型本身上完成。但是,您可以在模型上提供排序,并在标题上启用排序指示器。
- int sortIndicatorColumn-当前排序列的索引
- bool sortIndicatorVisible-是否应该启用排序指示器
- enum sortIndicatorOrder-Qt.AscendingOrder还是Qt.DescendingOrder取决于状态
你可以通过分配一个TableViewStyle来为TableView创建一个自定义外观。
下面的代码,演示通过itemDelegate设置表格样式:
import QtQuick 2.9
import QtQuick.Window 2.2
import QtQuick.Controls 2.2
import QtQuick.Controls 1.4
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
ListModel{
id:listModel
ListElement{title:"A";author:"Xiaoming"}
ListElement{title:"B";author:"Liling"}
ListElement{title:"C";author:"ABing"}
}
TableView{
anchors.fill: parent
model: listModel
TableViewColumn{
role:"title"
title: "标题"
width: 100
}
TableViewColumn{
role:"author"
title: "作者"
width: 200
}
itemDelegate: Item {
Text {
anchors.horizontalCenter: parent.horizontalCenter
color:styleData.selected? "red":"blue"
font.pixelSize: 10
text: styleData.column == 1? "column 1":styleData.value
}
}
onClicked: {
console.debug(row);
}
}
}

边栏推荐
- Fastadmin TP installation uses Baidu rich text editor ueeditor
- MySQL read / write lock
- Test Driven Development (TDD) online practice room | classes open on September 17
- 3D语义分割——PVD
- 百度富文本编辑器UEditor单张图片上传跨域
- Exclusive lock
- MySQL implicit lock
- 聊聊如何用 Redis 实现分布式锁?
- easyui下拉框,增加以及商品的上架,下架
- 【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
猜你喜欢

复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置

【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码

Which led display manufacturer is better

微信公众号开发之消息的自动回复

0x80131500 solution for not opening Microsoft Store

吴恩达逻辑回归2

聊聊如何用 Redis 实现分布式锁?

2W word detailed data Lake: concept, characteristics, architecture and cases

Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)

Sum arrays with recursion
随机推荐
Mqtt x cli officially released: powerful and easy-to-use mqtt 5.0 command line tool
MySQL global lock
Cookie、cookie与session区别
MySQL check whether the table is locked
easyui下拉框,增加以及商品的上架,下架
Intention lock
进程之间的通信(管道详解)
Paper notes: highly accurate protein structure prediction with alphafold (alphafold 2 & appendix)
2W word detailed data Lake: concept, characteristics, architecture and cases
在 NgModule 里通过依赖注入的方式注册服务实例
测试驱动开发(TDD)在线练功房 | 9月17日开课
3D语义分割——PVD
Google Earth Engine——全球建筑物GlobalMLBuildingFootprints矢量集合下载
2D semantic segmentation -- deeplabv3plus reproduction
Test framework unittest test test suite, results output to file
【云驻共创】探秘GaussDB如何助力工商银行打造金融核心数据
异常处理机制专题1
激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
Use huggingface to quickly load pre training models and datasets in moment pool cloud