当前位置:网站首页>Rosen's QT journey 99 QML table control tableview
Rosen's QT journey 99 QML table control tableview
2022-07-25 16:38:00 【Allen Roson】
TableView Be similar to QML Medium ListView, similar Widget Medium QTableView, It adds a scroll bar 、 Select and resize the title section . And ListView equally , Each row of data is provided through a model :
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: " title "
width: 100
}
TableViewColumn{
role:"author"
title: " author "
width: 200
}
}
}

The code above uses ListModel Provide data as a data model , One for each row of data ListElement Objects represent , It's specified here title and author Two fields . To use the data in the model , stay TableView Required in TableViewColumn To specify the fields in the model , every last TableViewColumn Corresponding to a field , Where you need to specify role That is, the field name in the model , and title Is the column header of each column to be displayed .TableViewColumn The type also contains visible、width resizable、mova-ble,delegate Equal attribute , among delegate Property can use delegates to specify the display method for specific columns , Specific properties can be accessed in delegates .
TableView Can pass itemDelegate attribute , Use delegates to draw specified cells . It is associated with TableViewColumn Of delegate The effect is the same . In addition, you can also use rowDelegate and headerDelegate To draw row or column headers using delegates .TableView Type also provides some commonly used functions , such as ad-dColumn() ,getColumn() 、 insertColumn() 、 removeColumn()、rowAt(x,y) etc. , Carry out relevant operations . in addition , When the line is clicked, it will launch clicked() or doubleClick() The signal .
TableView The view itself does not provide sorting . This must be done on the model itself . however , You can provide sorting on the model , And enable the sort indicator on the title .
- int sortIndicatorColumn- Index of the current rank
- bool sortIndicatorVisible- Whether the sort indicator should be enabled
- enum sortIndicatorOrder-Qt.AscendingOrder still Qt.DescendingOrder It depends on the State
You can assign one TableViewStyle for TableView Create a custom appearance .
The following code , demonstration adopt itemDelegate Set table style :
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: " title "
width: 100
}
TableViewColumn{
role:"author"
title: " author "
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);
}
}
}

边栏推荐
- Test framework unittest command line operation and assertion method
- 80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
- 如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序
- ILSSI认证|六西格玛DMAIC的历程
- Baidu rich text editor ueeditor image width 100% adaptive, mobile terminal
- MYSQL导入sqllite表格的两种方法
- Rebudget汇报PPT
- Sum arrays with recursion
- MySQL isolation level transactions
- Wechat applet does not use plug-ins, rendering videos in rich text, image adaptation, plus version
猜你喜欢

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

Talk about how to use redis to realize distributed locks?

Win11桌面切换快捷键是什么?Win11快速切换桌面的方法
![[zeloengine] summary of pit filling of reflection system](/img/7a/c85ba66c5dd05908b2d784fab306a2.png)
[zeloengine] summary of pit filling of reflection system

月薪1万在中国是什么水平?答案揭露残酷的收入真相

WPF 实现用户头像选择器

如何安装govendor并打开项目

Rebudget: balance efficiency and fairness in market-based multi-core resource allocation by reallocating the budget at run time

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心

Fudan University emba2022 graduation season - graduation does not forget the original intention and glory to embark on the journey again
随机推荐
Use huggingface to quickly load pre training models and datasets in moment pool cloud
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>
0x80131500打不开微软商店的解决办法
月薪1万在中国是什么水平?答案揭露残酷的收入真相
C# 音乐
【云驻共创】探秘GaussDB如何助力工商银行打造金融核心数据
The annualized interest rate of treasury bonds is too low. Is there a financial product with a higher annualized interest rate than the reverse repurchase of treasury bonds?
152. Product maximum subarray
easyui修改以及datagrid dialog form控件使用
unity 最好用热更方案卧龙 wolong
Rebudget汇报PPT
Test framework unittest command line operation and assertion method
ReBudget:通过运行时重新分配预算的方法,在基于市场的多核资源分配中权衡效率与公平性
Doget and dopost
Baidu rich text editor ueeditor image width 100% adaptive, mobile terminal
MYSQL导入sqllite表格的两种方法
如何使用 4EVERLAND CLI 在 IPFS 上部署应用程序
IaaS基础架构云 —— 云网络
Win11动态磁贴没了?Win11中恢复动态磁贴的方法
Register service instances in ngmodule through dependency injection