当前位置:网站首页>(十一)TableView
(十一)TableView
2022-06-10 21:01:00 【vegetablesssss】
TableView就是Qt Quick为了呈现出类似Excel表格样式数据而提供的组件,TableView和ListView类似,相比之下多了滚动条、挑选、可调整尺寸的表头等特性。它的数据也可以通过Model来提供,可以使用ListModel、XmlListModel,也可以使用C++中从QAbstractItemModel、QAbstractTableModel等继承而实现的Model。
实例
import QtQuick 2.2
import QtQuick.Controls 1.2
Rectangle {
width: 360;
height: 300;
TableView {
id: phoneTable;
anchors.fill: parent;
TableViewColumn {
role : "name";
title : "Name";
width : 100;
elideMode: Text.ElideRight;
}
TableViewColumn {
role : "cost";
title : "Cost";
width: 100;
}
TableViewColumn {
role : "manufacturer";
title : "Manufacturer";
width: 140;
}
model: ListModel {
id: phoneModel;
ListElement {
name : "iPhone 13";
cost : "9999";
manufacturer : "Apple";
}
ListElement {
name : "IQOO";
cost : "2099";
manufacturer : "VIVO";
}
ListElement {
name : "redMi";
cost : "899";
manufacturer : "XiaoMi";
}
ListElement {
name : "OnePlus";
cost : "2799";
manufacturer : "VIVO";
}
}
focus: true;
}
}

定制表格外观
通过设置itemDelegate,rowDelegate,headerDelegate等属性,可以定制表格的外观。
itemDelegate属性指定如何绘制每一个单元格,它的类型是Component,在itemDelegate中可以访问的styleData属性与TableViewColumn的delegate一样:
itemDelegate: Text{
text : styleData.value;
color : styleData.selected ? "red" : styleData.textColor;
elide : styleData.elideMode;
}
elide表示如果文本显示不全,就采用styleData.elideMode指定的省略模式,默认模式下省略号在右侧。
rowDelegate指定如何绘制行背景,它的高度降决定TabeView的行高,rowDelegate可以访问下列styleData属性:
styleData.alternate,本行使用交替的背景颜色时为true
styleData.selected,本行被选中时为true
styleData.row,本行的索引
rowDelegate : Rectangle{
color : styleData.selected ? root.highlight : (styleData.alternate ? root.alterBackground:root.background);
}
headerDelegate属性定义如何绘制表头,它可以访问下列styledata附加属性:
styleData.value :本item的值
styleData.column:本列索引
styleData.pressed:本列被按下(鼠标左键)时为true
styleData.containsMouse:鼠标时否停在本列内
styleData.textAlignment:本列文本的水平对齐方式
headerDelegate : Rectangle {
implicitWidth: 10;
implicitHeight: 24;
gradient: styleData.pressed ? root.pressG : (styleData.containsMouse ? root.hoverG : root.normalG);
border.width : 1;
border.color : "gray";
Text {
anchors.verticalCenter: parent.verticalCenter;
anchors.left: parent.left;
anchors.leftMargin: 4;
text : styleData.value;
color : styleData.pressed ? "red" : "blue";
font.bold : true;
}
}
修改一下上面tableview的例子,为tableview加上itemDelegate,rowDelegate,headerDelegate:
import QtQuick 2.2
import QtQuick.Controls 1.2
Rectangle {
width: 360;
height: 300;
id : root;
property var background: "#d7e3bc";
property var alterBackground: "white";
property var highlight: "#e4f7d6";
property var headerBkgnd: "#F0F0F0";
property var normalG: Gradient {
GradientStop{
position: 0.0; color : "#c7d3ac"}
GradientStop{
position: 1.0; color : "#F0F0F0"}
}
property var hoverG: Gradient {
GradientStop{
position: 0.0; color : "white"}
GradientStop{
position: 1.0; color : "#d7e3bc"}
}
property var pressG: Gradient {
GradientStop{
position: 0.0; color : "#d7e3bc"}
GradientStop{
position: 1.0; color : "white"}
}
TableView {
id: phoneTable;
anchors.fill: parent;
TableViewColumn {
role : "name";
title : "Name";
width : 100;
elideMode: Text.ElideRight;
}
TableViewColumn {
role : "cost";
title : "Cost";
width: 100;
}
TableViewColumn {
role : "manufacturer";
title : "Manufacturer";
width: 140;
}
itemDelegate : Text {
text : styleData.value;
color: styleData.selected ? "red" : styleData.textColor;
elide: styleData.elideMode;
}
rowDelegate: Rectangle {
color: styleData.selected ? root.highlight : (styleData.alternate ? root.alterBackground: root.background);
}
headerDelegate: Rectangle {
implicitWidth: 10;
implicitHeight: 24;
gradient: styleData.pressed ? root.pressG : (styleData.containsMouse ? root.hoverG : root.normalG);
border.width : 1;
border.color : "gray";
Text {
anchors.verticalCenter: parent.verticalCenter;
anchors.left: parent.left;
anchors.leftMargin: 4;
text : styleData.value;
color : styleData.pressed ? "red" : "blue";
font.bold : true;
}
}
model: ListModel {
id: phoneModel;
ListElement {
name : "iPhone 13";
cost : "9999";
manufacturer : "Apple";
}
ListElement {
name : "IQOO";
cost : "2099";
manufacturer : "VIVO";
}
ListElement {
name : "redMi";
cost : "899";
manufacturer : "XiaoMi";
}
ListElement {
name : "OnePlus";
cost : "2799";
manufacturer : "VIVO";
}
}
focus: true;
}
}

动态修改TableView
有时可能会给TableView添加一列,这时可以用addColumn方法,其参数是TableViewColumn,指向一个TableViewColumn实例,你可以动态创建这个实例。
Component.onCompleted:{
var col = Qt.createQmlObject("import QtQuick 2.2\nimport QtQuick.Controls 1.2\nTableViewColumn{
role: \"manufacturer\";title: \"Manufactured\";width:140;
}",phoneModel);
phoneTable.addColumn(col);
}
要想在指定位置添加一列,可以使用insertColumn(index,column)方法,index参数指定列索引,column参数与addColumn()的参数一样。
要想删除某列,可以使用removeColumn(index)方法。
TableView还提供了moveColumn(from,to),用于降一列从位置from移动到to。
如果想给TableView动态添加数据,则可以通过调用ListModel的append()或insert()方法实现,删除数据通过ListModel的clear()或remove()方法实现。
对于自定义的Model,则需要提供增删改接口。
边栏推荐
- [nk] Niuke monthly competition 51 f-average question
- 【C#】overide可重写从更高父级继承来的virtual方法
- PMP candidates, there are these places for PMP examination in Shenzhen in June 2022
- 【MySQL】常见数据类型总结
- 数组 移动0
- 19 MySQL optimizations commonly used in projects
- An analysis of SQL query optimization principle (900w+ data from 17s to 300ms)
- Ajout, suppression et modification des données du tableau [MySQL] (DML)
- 笔记(五)- JVM
- Has the samesite cookie problem occurred when using identityserver?
猜你喜欢

Course design of imitation pottery ticket of wechat applet

C language - quick sorting in sorting

KDD2022 | 基于对抗性知识蒸馏的深度图神经网络压缩

2022-06-09 rk817 PMU battery temperature detection
![[nk] Niuke monthly competition 51 f-average question](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] Niuke monthly competition 51 f-average question

数组 求并集

【phpstorm】 No data sources are configured to run this SQL and provide advanced c

ThinkPHP v6.0.x反序列化漏洞复现

2022-06-09 RK817 PMU 电池温度检测
![[nk] 牛客月赛51 F-平均题](/img/b3/c36a0032e606f38fdc2f7c4562713c.png)
[nk] 牛客月赛51 F-平均题
随机推荐
数组 求并集
Detailed steps and actual records of SQL server2019 installation (available for hands-on test)
SQL Server row to column (pivot), column to row (unpivot)
Principle of gravure overprint and factors affecting overprint
Are you still writing the TS type code
ArrayList的扩容机制
Share some information I accumulated when I was working as a dotnet9 blog site
Abbexa AML1 DNA binding ELISA Kit instructions
数组 从指定长度位旋转数组
String inversion
记录(三)
在Oracle表中进行关键词搜索的过程
Mysql之將查詢結果插入到其它錶中
C language - quick sorting in sorting
笔记(四)- 多线程
C语言qsort()函数的使用(详解)
Apache相关的几个安全漏洞修复
Abbexa cdan1 siRNA instruction manual
C language -- 7 operators
Only this is the most true reason why leaders promote you. The rest is nonsense!