网易首页 > 网易号 > 正文 申请入驻

QML TableView表格使用示例

0
分享至

前言

QML中实现表格可以使用多种方式,比如直接使用ListView,定义每一行delegate,或者自定义Rectangle,放到Flipable中组合使用。Qt Quick Control1中 从5.1版本开始就提供了表格控件,但是感觉不怎么好用,在Qt Quick Control2中 5.12版本开始又提供了一个专门用于做表格的控件TableView,相比于前面的方案,使用Tableview更加简单和直接。那么,接下来就看看Quick Control2 的TableView使用方法。

正文

我们直接针对两种常用的场景来通过示例说明。

场景一

第一种场景, 每一列等宽,然后内容都是一致的(比如全是文本Text),这种无需定制每一列的delegate,所以只需要写一次即可。

示例:

代码:

import QtQuick 2.13import QtQuick.Controls 2.13import Qt.labs.qmlmodels 1.0Rectangle {Rectangle{id:headerwidth: parent.widthheight: 30Row{spacing: 0Repeater{model: ["name","age","class","number"]Rectangle{width: header.width/4height: header.heightcolor: "#666666"border.width: 1border.color: "#848484"Text {text: modelDataanchors.centerIn: parentfont.pointSize: 12color: "white"}}}}}TableView{id:tableViewwidth: parent.widthanchors.top:header.bottomanchors.left: parent.leftanchors.right: parent.rightanchors.bottom: parent.bottomclip: trueboundsBehavior: Flickable.OvershootBoundsScrollBar.vertical: ScrollBar {anchors.right:parent.rightanchors.rightMargin: 0visible: tableModel.rowCount > 5background: Rectangle{color:"#666666"}onActiveChanged: {active = true;}contentItem: Rectangle{implicitWidth: 6implicitHeight : 30radius : 3color: "#848484"}}model: TableModel {id:tableModelTableModelColumn{display: "name"}TableModelColumn{display: "age"}TableModelColumn{display: "class"}TableModelColumn{display: "number"}}delegate:Rectangle{color: "#666666"implicitWidth: tableView.width/4implicitHeight: 32border.width: 1border.color: "#848484"Text {text: displayanchors.centerIn: parentfont.pointSize: 12color: "white"}}}Component.onCompleted: {tableModel.appendRow({"name":"小明","age":12,"class":"三年二班","number":"003"})tableModel.appendRow({"name":"小刚","age":13,"class":"三年三班","number":"012"})tableModel.appendRow({"name":"小李","age":11,"class":"三年四班","number":"023"})tableModel.appendRow({"name":"小王","age":10,"class":"三年二班","number":"021"})tableModel.appendRow({"name":"小张","age":13,"class":"三年五班","number":"004"})tableModel.appendRow({"name":"小林","age":14,"class":"三年一班","number":"008"})}}

场景二

场景二,每一列内容不一样,需要单独定制

如图:

代码:

import QtQuick 2.13import QtQuick.Controls 2.13import Qt.labs.qmlmodels 1.0Rectangle {Rectangle{id:headerwidth: parent.widthheight: 30Row{spacing: 0Repeater{model: ["name","sex","id","option"]Rectangle{width: {var w = 0switch(index){case 0: w = 140;break;case 1: w = 90;break;case 2: w = 120;break;case 3: w = 150;break;}return w}height: header.heightcolor: "#666666"border.width: 1border.color: "#848484"Text {text: modelDataanchors.centerIn: parentfont.pointSize: 12color: "white"}}}}}TableView{id:tableViewwidth: parent.widthanchors.top:header.bottomanchors.left: parent.leftanchors.right: parent.rightanchors.bottom: parent.bottomclip: trueboundsBehavior: Flickable.OvershootBoundsScrollBar.vertical: ScrollBar {anchors.right:parent.rightanchors.rightMargin: 0visible: tableModel.rowCount > 5background: Rectangle{color:"#666666"}onActiveChanged: {active = true;}contentItem: Rectangle{implicitWidth: 6implicitHeight : 30radius : 3color: "#848484"}}model: TableModel {id:tableModelTableModelColumn{display: "name"}TableModelColumn{display: "sex"}TableModelColumn{display: "id"}TableModelColumn{display: "option"}}delegate:DelegateChooser{DelegateChoice{column: 0delegate: Rectangle{color: "#666666"implicitWidth: 140implicitHeight: 32border.width: 1border.color: "#848484"Text {text: displayanchors.centerIn: parentfont.pointSize: 12color: "blue"}}}DelegateChoice{column: 1delegate: Rectangle{color: "#666666"implicitWidth: 90implicitHeight: 32border.width: 1border.color: "#848484"Text {text: displayanchors.centerIn: parentfont.pointSize: 12color: "white"}}}DelegateChoice{column: 2delegate: Rectangle{color: "#666666"implicitWidth: 120implicitHeight: 32border.width: 1border.color: "#848484"clip: trueText {text: displayanchors.centerIn: parentfont.pointSize: 12color: "white"}}}DelegateChoice{column: 3delegate: Rectangle{color: "#666666"implicitWidth: 150implicitHeight: 32border.width: 1border.color: "#848484"Button{width: 70height: 25anchors.centerIn: parenttext: "Delete"background: Rectangle{radius: 4color: "cyan"}onClicked: {console.log("btn clicked row:",row)}}}}}}Component.onCompleted: {tableModel.appendRow({"name":"小明","sex":"男","id":"w0000065628743342144321241","option":true})tableModel.appendRow({"name":"小刚","sex":"女","id":"w0000065628743342144312312","option":true})tableModel.appendRow({"name":"小李","sex":"男","id":"w0000065628743342144315433","option":true})tableModel.appendRow({"name":"小王","sex":"男","id":"w0000065628743342144313254","option":true})tableModel.appendRow({"name":"小张","sex":"女","id":"w0000065628743342144316573","option":true})tableModel.appendRow({"name":"小林","sex":"男","id":"w0000065628743342144311232","option":true})}}

解决表格中文字显示不下的问题

从上面场景二的示例中看到第三列文字显示不下,这种情况下,通常会将文字省略显示,超出部分用“…”代替,然后做tips,实现方式如下:

当鼠标移动到文字上时做ToolTip

代码,修改第三列:

DelegateChoice{ column: 2 delegate: Rectangle{ id:rect color: "#666666" implicitWidth: 120 implicitHeight: 32 border.width: 1 border.color: "#848484" clip: true TextMetrics { id: textMetrics text: display } Text { text: display anchors.centerIn: parent font.pointSize: 12 color: "white" width: parent.width elide: Text.ElideRight leftPadding: 3 rightPadding: 3 MouseArea{ id:ma hoverEnabled: true anchors.fill: parent } ToolTip { height: 26 visible: ma.containsMouse && display !== "" && textMetrics.width > (rect.implicitWidth-6) contentItem: Text { text: display color: "#D6D6D6" } background: Rectangle { color: "#222222" } } } } }

【领QT开发教程学习资料,点击下方链接莬费领取↓↓,先码住不迷路~】

点击这里:

「链接」

特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。

Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.

相关推荐
热点推荐
不要陈国豪+拒绝朱松玮!广东最心仪的锋线曝光,或被朱芳雨招募

不要陈国豪+拒绝朱松玮!广东最心仪的锋线曝光,或被朱芳雨招募

绯雨儿
2024-06-02 11:46:11
落月实况来了!嫦娥六号月背着陆影像发布

落月实况来了!嫦娥六号月背着陆影像发布

第一财经资讯
2024-06-02 14:38:27
爸妈让我拿600000给弟弟买房,老公知道后,拉着我就去了娘家

爸妈让我拿600000给弟弟买房,老公知道后,拉着我就去了娘家

我是三月鱼H
2024-06-01 18:10:34
你是否“中午不睡,魂归床畔”?多项研究表明:经常午睡会导致高血压风险升高12%,且午睡不宜超过60分钟

你是否“中午不睡,魂归床畔”?多项研究表明:经常午睡会导致高血压风险升高12%,且午睡不宜超过60分钟

梅斯医学
2024-06-02 07:52:31
A股最后的提醒,主力已经明牌了,明天周一的剧本已经出炉

A股最后的提醒,主力已经明牌了,明天周一的剧本已经出炉

静守时光落日
2024-06-02 11:40:24
张朝阳:年轻人一旦负债,这辈子没法往下走了!评论全是悔恨之声

张朝阳:年轻人一旦负债,这辈子没法往下走了!评论全是悔恨之声

芯怡飞
2024-05-27 13:41:21
笑不活了!妈妈记录姐弟俩干架日常上热搜,我却笑死在评论区里!

笑不活了!妈妈记录姐弟俩干架日常上热搜,我却笑死在评论区里!

四象八卦
2024-06-01 15:34:58
价格暴跌!从70元一斤跌到个位数,正大量上市

价格暴跌!从70元一斤跌到个位数,正大量上市

浙江之声
2024-05-30 08:32:08
6万元买两室一厅,想要“白菜价”不用跑鹤岗?张家口出现价格“个位数”房源

6万元买两室一厅,想要“白菜价”不用跑鹤岗?张家口出现价格“个位数”房源

华夏时报
2024-06-01 23:32:06
中国制裁有没有用?看看今天的洛马就知道了,什么叫“慢性死亡”

中国制裁有没有用?看看今天的洛马就知道了,什么叫“慢性死亡”

李律讲法
2024-06-02 15:15:02
再见,皇马!曝“落寞先生”转投曼城!头号猎物遭标价8000万欧

再见,皇马!曝“落寞先生”转投曼城!头号猎物遭标价8000万欧

头狼追球
2024-06-02 09:36:57
普京发出警告

普京发出警告

新京报
2024-05-30 12:09:07
死一个菲律宾人,中菲就开战?小马科斯的口嗨,被美防长泼了冷水

死一个菲律宾人,中菲就开战?小马科斯的口嗨,被美防长泼了冷水

娱乐的小灶
2024-06-02 17:53:12
菲律宾打响首枪,仙宾礁斗争质变,中方船只越聚越多,反包围开始

菲律宾打响首枪,仙宾礁斗争质变,中方船只越聚越多,反包围开始

文雅笔墨
2024-06-01 19:26:44
3-0!神仙球打服华裔美女,孙颖莎取7连胜,下轮有望中日一姐对决

3-0!神仙球打服华裔美女,孙颖莎取7连胜,下轮有望中日一姐对决

钉钉陌上花开
2024-06-01 21:47:10
突发!GDP从5.2%大跌至1.3%,美国收割失败,大败已成定局

突发!GDP从5.2%大跌至1.3%,美国收割失败,大败已成定局

小马哥谈体育
2024-06-02 17:32:00
799 美元,华硕 ROG Ally X 游戏掌机发布:80Wh 电池、24GB 内存

799 美元,华硕 ROG Ally X 游戏掌机发布:80Wh 电池、24GB 内存

IT之家
2024-06-02 17:18:12
汪峰与森林北一起过儿童节!逛商店主动付款,默契度似老夫老妻

汪峰与森林北一起过儿童节!逛商店主动付款,默契度似老夫老妻

阿美的深秋感悟
2024-06-02 17:03:49
“欠中国的钱全还清了”,公开力挺中国,大量天然气将直达我国

“欠中国的钱全还清了”,公开力挺中国,大量天然气将直达我国

星辰故事屋
2024-05-28 19:23:27
美媒曾警告:若台海冲突,解放军敢攻击美航母,美导弹将降落中国

美媒曾警告:若台海冲突,解放军敢攻击美航母,美导弹将降落中国

清欢渡语
2024-05-31 22:03:04
2024-06-02 18:56:49
老趣觅食
老趣觅食
喜欢做各种各种好吃的
1394文章数 1684关注度
往期回顾 全部

科技要闻

成功着陆!嫦娥六号将开始月背“挖宝”

头条要闻

媒体:中美防长会谈细节披露 董军的两句话值得注意

头条要闻

媒体:中美防长会谈细节披露 董军的两句话值得注意

体育要闻

我已伤痕累累 却依然感动不了命运之神

娱乐要闻

白玉兰提名:胡歌、范伟争视帝

财经要闻

新造车5月销量: 小鹏乏力 问界暂"缺席"

汽车要闻

吉利银河E5 Flyme Auto智能座舱首发

态度原创

健康
家居
教育
游戏
军事航空

晚餐不吃or吃七分饱,哪种更减肥?

家居要闻

风雅自来 中式的和谐平衡

教育要闻

放假的时候逼孩子早起是一种“病”,非得让孩子不舒服来体现自己的存在感和优越感

Rookie赛场为患癌粉丝佩戴蓝丝带 温暖真情令人泪目

军事要闻

匈牙利总理:欧洲已进入对俄开战准备阶段

无障碍浏览 进入关怀版