Qt qml treeview 树控件
qml并没有提供树控件,只能自己写了。model仍然用ListModel对象,弄成层级的就行。delegate必须用loader动态的增加子控件,如此而已。
【先看效果】

【下载】
http://download.csdn.net/detail/surfsky/8406181
【核心代码】
import QtQuick 2.1
import QtQuick.Controls 1.0 /**
树控件
作者:surfsky.cnblogs.com 2014-10
协议:MIT 请保留本文档说明
功能
/递归树显示
/左侧一个箭头,点击可展开显示子树
/选中节点变色
/节点点击事件
/tag属性,携带类似id的数据
异步方式,点击箭头后请求子数据。异步模式的话,节点要加上isLeaf属性,点击箭头后动态加载数据
使用
TreeView {
anchors.fill: parent
id: tree
model: modelTree2
onSelectedItemChanged: console.log(item.text)
}
ListModel {
id: modelTree2
Component.onCompleted: {
modelTree2.append([
{title: "Node 1"},
{title: "Node 2", items: [
{title: "Node 21", items:[
{title: "Node 211"},
{title: "Node 212"}
]},
{title: "Node 22"}
]
},
{title: "Node 3"}
]);
}
}
参考 http://qt-project.org/forums/viewthread/30521/
*/
ScrollView {
id: view
frameVisible: true
implicitWidth: 200
implicitHeight: 160 // 输入属性
property var model
property int rowHeight: 19
property int columnIndent: 22
property string expanderImage : "expander.png"; // 私有属性
property var currentNode // 当前节点数据
property var currentItem // 当前节点UI // 信号
signal selectedItemChanged(var item) // 节点数据展示的UI
property Component delegate: Label {
id: label
text: model.title ? model.title : 0
color: currentNode === model ? "white" : "black"
property var tag : model.tag
} //
contentItem: Loader {
id: content
onLoaded: item.isRoot = true
sourceComponent: treeBranch
property var items: model // 背景条纹
Column {
anchors.fill: parent
Repeater {
model: 1 + Math.max(view.contentItem.height, view.height) / rowHeight
Rectangle {
objectName: "Faen"
color: index % 2 ? "#eee" : "white"
width: view.width ; height: rowHeight
}
}
} // 树节点组件
Component {
id: treeBranch
Item {
id: root
property bool isRoot: false
implicitHeight: column.implicitHeight
implicitWidth: column.implicitWidth
Column {
id: column
x: 2
Item { height: isRoot ? 0 : rowHeight; width: 1}
Repeater {
model: items
Item {
id: filler
width: Math.max(loader.width + columnIndent, row.width)
height: Math.max(row.height, loader.height)
property var _model: model
// 当前行背景色块
Rectangle {
id: rowfill
x: view.mapToItem(rowfill, 0, 0).x
width: view.width
height: rowHeight
visible: currentNode === model
color: "#37f"
}
// 行点击响应区域
MouseArea {
anchors.fill: rowfill
onPressed: {
currentNode = model
currentItem = loader
forceActiveFocus()
selectedItemChanged(model);
}
}
// 行数据UI
Row {
id: row
// 行图标
Item {
width: rowHeight
height: rowHeight
opacity: !!model.items ? 1 : 0
Image {
id: expander
source: view.expanderImage
height: view.rowHeight * 0.6
fillMode: Image.PreserveAspectFit
opacity: mouse.containsMouse ? 1 : 0.7
anchors.centerIn: parent
rotation: loader.expanded ? 90 : 0
Behavior on rotation {NumberAnimation { duration: 120}}
}
MouseArea {
id: mouse
anchors.fill: parent
hoverEnabled: true
onClicked: loader.expanded = !loader.expanded
}
}
// 行文本
Loader {
property var model: _model
sourceComponent: delegate
anchors.verticalCenter: parent.verticalCenter
}
} // 子树(递归自身)
Loader {
id: loader
x: columnIndent
height: expanded ? implicitHeight : 0
property var node: model
property bool expanded: false
property var items: model.items
property var text: model.title
sourceComponent: (expanded && !!model.items) ? treeBranch : undefined
}
}
}
}
}
}
}
}
Qt qml treeview 树控件的更多相关文章
- Qt实现表格树控件-自绘树节点虚线
目录 一.开心一刻 二.自绘树节点? 三.效果展示 四.实现思路 1.可扩展接口 2.函数重写 3.同步左侧表头 五.相关文章 原文链接:Qt实现表格树控件-自绘树节点虚线 一.开心一刻 一程序员第一 ...
- Qt实现表格树控件-支持多级表头
目录 一.概述 二.效果展示 三.实现方式 四.多级表头 1.数据源 2.表格 3.QStyledItemDelegate绘制代理 五.测试代码 六.相关文章 原文链接:Qt实现表格树控件-支持多级表 ...
- MVC树控件,mvc中应用treeview,实现复选框树的多层级表单控件
类似于多层级的角色与权限控制功能,用MVC实现MVC树控件,mvc中应用treeview,实现复选框树的多层级表单控件.最近我们的项目中需要用到树型菜单,以前使用WebForm时,树型菜单有微软提供的 ...
- WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接. 本文主要内容: 菜单M ...
- 【转】WPF自定义控件与样式(9)-树控件TreeView与菜单Menu-ContextMenu
一.前言 申明:WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等. 本文主要内容: 菜单Menu的自定义样式: 右键菜单ContextMenu的自定义样式 ...
- 表格树控件QtTreePropertyBrowser编译成动态库(设计师插件)
目录 一.回顾 二.动态库编译 1.命令行编译动态库和测试程序 2.vs工具编译动态库和测试程序 3.安装文档 4.测试文档 三.设计师插件编译 1.重写QDesignerCustomWidgetIn ...
- 超级实用的表格树控件--QtTreePropertyBrowser
目录 一.源码下载 二.代码编译 1.intersect函数替换为intersected 2.移除UnicodeUTF8 3.QtGui模块拆分 4.Q_TYPENAME错误 5.qVariantVa ...
- PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)
1.树控件的基本使用方法QTreeWidget'''QTreeWidget树控件的使用方法添加图标,添加表格,添加复选框等'''from PyQt5.QtWidgets import *from Py ...
- JS组件系列——Bootstrap 树控件使用经验分享
前言:很多时候我们在项目中需要用到树,有些树仅仅是展示层级关系,有些树是为了展示和编辑层级关系,还有些树是为了选中项然后其他地方调用选中项.不管怎么样,树控件都是很多项目里面不可或缺的组件之一.今天, ...
随机推荐
- 【emWin】例程五:显示数值
实验指导书及代码包下载: 链接:http://pan.baidu.com/s/1pLexsAf密码:p0jf 实验现象:
- 数据库使用数据泵迁移遇到LOB字段
impdp system/Clic1234 attach=SYS_IMPORT_ILEARN_TRA desc ILEARN_TRA.NOTIFI_TACTIC desc ILEARN_TRA.MSG ...
- java并发容器类
本文主要介绍java并发容器相关实现类,collections节点下接口方法介绍. Queue Java提供的线程安全的Queue可以分为阻塞队列和非阻塞队列,其中阻塞队列的典型例子是Blocking ...
- REACT day 1
https://facebook.github.io/react/ A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES Declarative view ...
- Codeigniter 3.0 相关文档 part two
分页 首先,配置 $this->load->library('pagination'); $config = array(); // $this->config->load(' ...
- 手动删除portal中托管服务。
在portal中将server作为托管联合服务器,然后发布了托管服务.若中间取消了托管联合服务器,再重新连接,那么会出现之前的托管服务无法删除的现象. 下文为怎样手动删除这些服务的方法,(不过貌似之后 ...
- ASP.NET Web API 2基于令牌的身份验证
基于令牌的认证 我们知道WEB网站的身份验证一般通过session或者cookie完成的,登录成功后客户端发送的任何请求都带上cookie,服务端根据客户端发送来的cookie来识别用户. WEB A ...
- linux实践之程序破解
linux实践之程序破解 这次的实践是文件破解,让我们从login可执行文件开始吧! 首先我们执行一下这个可执行程序 ①我们希望在不知道密码的情况下,能够登陆进去.且无论密码是什么,都是提示“on y ...
- drop表后仍占表空间解决办法
练习oracle时create了很多表,drop表后select * from tab; 网上找了好些方法,但是好多都适用... SQL>purge recyclebin; 回收站已清空.
- SQLite - TRUNCATE TABLE
https://www.tutorialspoint.com/sqlite/sqlite_truncate_table.htm Unfortunately, no TRUNCATE TABLE in ...