当前位置: 首页 > news >正文

使用QML的ListView自制树形结构图TreeView

背景

感觉QML自带的TreeView不是很好用,用在文件路径树形结构比较多,但是想用在自己数据里,就不太方便了,所以自己做一个。

用‘ListView里迭代ListView’的方法,制作树形结构,成果图:
在这里插入图片描述

代码

新建一个MyTreeView.qml用来写主要代码,再在main.qml中调用MyTreeView,并为它填充数据。
这里数据是在qml中用JSON.parse构建的json数据,也可以在cpp中用QJsonArray和QJsonObject来构建数据。

// MyTreeView.qml
import QtQuick 2.0Item {id: rootproperty alias model: list_view.modelListView {id: list_viewwidth: contentWidthanchors.fill: parentanchors.margins: 10delegate: list_delegate}Component {id: list_delegateItem{id: list_itemgroupwidth: leftLine.width + centre.width + rightLine.width + listView.widthheight: Math.max(centre.height, listView.height)property int lineY: leftLine.yRectangle {id: leftLinewidth: 20height: 1color: "#d3dee4"visible: modelData.id >= 0anchors.verticalCenter: centre.verticalCenter}Rectangle {id: centrewidth: 150height: 50color: "#d3dee4"radius: 5anchors.left: leftLine.rightanchors.verticalCenter: parent.verticalCenterText {text: qsTr(modelData.name)font.pixelSize: 20font.bold: trueanchors.centerIn: parent}}Rectangle {id: rightLinewidth: 20height: 1color: "#d3dee4"visible: modelData.subnodes.length > 0anchors.verticalCenter: centre.verticalCenteranchors.left: centre.right}Rectangle {id: verticalLinewidth: 1color: "#d3dee4"x: parent.width + 1visible: modelData.subnodes.length > 1anchors.top: parent.topanchors.bottom: parent.bottom}ListView {id: listViewdelegate: list_delegateheight: contentHeightwidth: contentWidthanchors.right: parent.rightmodel: modelData.subnodesspacing: 10onModelChanged: {}}Component.onCompleted: {listView.forceLayout()  // 要先确保listView加载子项完成// 画一下verticalLine的y坐标起点和终点for (var i = 0; i < modelData.subnodes.length; i++) {var item = listView.itemAtIndex(i)if (i === 0) {verticalLine.anchors.topMargin = item.lineY} else if (i === modelData.subnodes.length-1) {verticalLine.anchors.bottomMargin = item.lineY}}}}}
}
// main.qml
import QtQuick 2.15
import QtQml.Models 2.15
import QtQuick.Window 2.12
import QtQuick.Controls 1.4Window {id: window_width: 940height: 680visible: truetitle: qsTr("Hello World")color: "#103e6f"Item {id: homeanchors.fill: parentMyTreeView {id: treeViewanchors.fill: parent}}Component.onCompleted: {var data = JSON.parse('[{"id": -1,"name": "目录","subnodes": [{"id": 0,"name": "第一本书","subnodes": [{"id": 1,"name": "第一章","subnodes": [{"id": 2,"name": "第一节","subnodes": []}, {"id": 3,"name": "第二节","subnodes": []}]}, {"id": 4,"name": "第二章","subnodes": [{"id": 5,"name": "第一节","subnodes": []}, {"id": 6,"name": "第二节","subnodes": []}, {"id": 7,"name": "第三节","subnodes": []}]}]}, {"id": 8,"name": "第二本书","subnodes": [{"id": 9,"name": "第一章","subnodes": [{"id": 10,"name": "第一节","subnodes": []}, {"id": 11,"name": "第二节","subnodes": []}]}, {"id": 12,"name": "第二章","subnodes": [{"id": 13,"name": "第一节","subnodes": []}]}]}]}]')treeView.model = data}}
http://www.lryc.cn/news/416372.html

相关文章:

  • 2.MySQL面试题之索引
  • 复制CodeIgniter新版的array_group_by辅助函数
  • 合并两个 ES (Elasticsearch) 的数据
  • Linux网络协议.之 tcp,udp,socket网络编程(四).之网络转换函数htonl,ntohs等介绍
  • LXC和udev知识点
  • 基于springboot+vue+uniapp的智慧校园管理系统小程序
  • 论文辅导 | 基于概率密度估计与时序Transformer网络的风功率日前区间预测
  • 金蝶云星空单据体数量汇总-分组列信息
  • 树状数组基础知识以及相关习题
  • 2023大数据-架构师案例(八)
  • 【Python】Python:探索未来科技的风向标
  • Java语言程序设计——篇十一(6)
  • 2024年有哪些好用的文件加密软件?十款常用加密软件推荐
  • 书生大模型学习笔记3 - 书生开源大模型链路体系
  • 【竞技宝】奥运会:法国国奥淘汰埃及国奥晋级决赛
  • C++的STL简介(四)
  • NIO专题学习(一)
  • Linux学习笔记:Linux基础知识汇总(个人复习版)
  • MSR020/MSR040低温漂、低功耗电压基准
  • 一个是生产打包的时候, 一个是本地测试启动的时候,maven如何配置?
  • 公文字体包下载
  • 主从备份及安装准备
  • 翻译英文的软件,分享3款翻译神器!
  • 软件测试解读——性能效率测试
  • 【PLC】子程序功能心得
  • Iris for mac 好用的录屏软件
  • Transformers实战05-模型量化
  • 【Python】bytes 和 bytearray 到底是什么类型呢?
  • Windows10上安装SQL Server 2022 Express
  • C++11 异常