技术标签: QML Drag DropArea TreeView
前段时间工作需要使用QML的TreeView,要通过拖动节点,对应节点执行对应的操作,查了很多的资料,没有看到关于节点可拖动的资料,查看TreeView的源码,貌似存在关于节点拖动的地方,但是始终没有看到可以使用的接口,只好自己动手造轮子了,加上使用过程中踩了不少的坑,也算是给后面用到的人一个案例吧,如果有什么好的建议,什么疑问,又或者有好的萝卜坑推荐,都可以加这个QQ:995187021,即使没有解决问题,也可以交个朋友。
import QtQuick 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQml.Models 2.2
Rectangle{
property variant nodePic: ["qrc:/pic/pan.png","qrc:/pic/dir.png","qrc:/pic/file.png"]
id:root
TreeView{
id:myTree
anchors.fill:parent
style: treeViewStyle
selection: sel
headerVisible: false //隐藏列的头,想知道作用可以注释掉运行看效果
Component.onCompleted: {
model = TreeModel.model() //从TreeModel获取model
}
itemDelegate:Item{ //节点代理
id:treeItem
Image { //节点前的图片
id: nodeImg
height: parent.height
width: { //不同级的节点图片宽度不一样
if(styleData.depth==0){
return parent.width/6
}else if(styleData.depth==1){
return parent.width/10
}else if(styleData.depth==2){
return parent.width/6
}
}
source: { //不同级的节点图片不一样
if(styleData.depth==0){
return nodePic[0]
}else if(styleData.depth==1){
return nodePic[1]
}else if(styleData.depth==2){
return nodePic[2]
}
}
}
Text{
id:itemText
anchors.left: nodeImg.right
anchors.leftMargin: 4
anchors.bottom:parent.bottom
// elide: styleData.elideMode
text:styleData.value //显示来自model的文本
color: styleData.selected ? "#007dff":"white" //选中时字体颜色切换
font.pointSize:styleData.selected ? 10:9 //选中时字体大小改变
Drag.active: styleData.depth<=1 ? false:itemMosue.drag.active;//一级节点不可拖动
Drag.supportedActions: Qt.CopyAction; //选择复制数据到DropArea
Drag.dragType: Drag.Automatic; //选择自动开始拖动
Drag.mimeData: {"text": text}; //选择要传的数据,这里传文本
MouseArea{ //节点代理的鼠标事件
id:itemMosue
hoverEnabled: true
anchors.fill: parent
drag.target: itemText
Drag.onDragStarted: { //控制台打印开始拖动
console.log("start")
}
onPressed: {
sel.setCurrentIndex(styleData.index,0x0010) //点击了文字,选中该节点
if(styleData.isExpanded){ //切换节点的展开状态
myTree.collapse(styleData.index)
}
else{
myTree.expand(styleData.index)
}
}
//onReleased: parent.Drag.drop()
}
}
}
TableViewColumn { //列
title:qsTr("所有通道")
role: "text" //显示的元素
width: 200 //列的宽
}
}
ItemSelectionModel { //自定义添加选中
id: sel
model: TreeModel.model();
}
Component {
id:treeViewStyle //树的自定义样式
TreeViewStyle {
indentation: 30 //节点间隔
branchDelegate: Image { //节点的展开标记图
id:image
source: styleData.isExpanded ? "qrc:/pic/collapse.png" : "qrc:/pic/expansion.png"
width: 9
height:9
anchors.top:parent.top
anchors.topMargin: 2
}
rowDelegate: Rectangle { //行代理
height: 16
color:styleData.selected? "#e9fffd" : "#323232" //这里决定了节点选中的颜色和背景颜色
}
}
}
}
import QtQuick 2.7
import QtQuick.Window 2.2
Window {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
MyTreeView{
id:myTree
height: parent.height
width: parent.width/2
}
Rectangle{
id:dropContainer
height: parent.height/2
width: parent.width/4
anchors.left: myTree.right
y:10
Text {
id: accptedText
text: qsTr("请拖动树节点到该矩形框!!!")
color: "red"
}
border.color: "red"
border.width: 1
DropArea{
id:myDropArea
anchors.fill: parent
onDropped: {
if(drop.supportedActions == Qt.CopyAction){
accptedText.text=drop.getDataAsString("text")
}
}
}
}
}
压缩包下载链接:
1.GitHub:https://github.com/simplewen/QML
2CSDN下载:https://download.csdn.net/download/weixin_40912639/10778343
ramdisk.img system.img userdata.img 分析: http://blog.csdn.net/wuye110/article/details/8463820
1、切换本地分支git checkout 本地分支[email protected] MINGW64 /e/idea_workspace/smart-medical (master)$ git checkout zzg-masterSwitched to branch 'zzg-master'M .idea/sonarlint/issuestore/c/4/c4287d63e4d7d93045c9448f2aeabe1a6f1fc897M .idea/sonar
2019独角兽企业重金招聘Python工程师标准>>> ...
1. 注册所有容器格式和CODEC:av_register_all()2. 打开文件:av_open_input_file()3. 从文件中提取流信息:av_find_stream_info()4. 穷举所有的流,查找其中种类为CODEC_TYPE_VIDEO5. 查找对应的解码器:avcodec_find_decoder()6. 打开编解码器:avcode
sigprocmask函数:功能描述:设定对信号屏蔽集内的信号的处理方式(阻塞或不阻塞)。用法:#include int sigprocmask(int how, const sigset_t *set, sigset_t *oldset);NOTE: If oldset is non-null, the previous value of the si
CamSim是一个灵活的高性能相机模拟器,可为图像采集卡生成Cameral Link或CoaXPress视频流和测试图案。该系统支持所有Camera Link规范v2.0配置、CoaXPress标准规范1.1版和任何所需的用户接口。该相机模拟器可以实现在一个低成本的安静的实验室环境中进行大部分机器视觉的项目开发。因此,CamSim可以极大地提高生产力,降低了开发视觉和成像系统的总体费用。同时CamSim数据流重复能力确保算法得到验证,并在相关的输入下按预期工作。此外,一旦检测到一个罕见的错误,其各自
安霸平台编译过程中提示:error while loading shared libraries: libjpeg.so.62: cannot open shared object file: No such file or directory执行sudo apt-get install libjpeg62后仍提示找不到libjpeg.so.62,后来发现ubuntu 64位系统下默认
boost升压电路(boost converter or step-up converter)是一种常见的开关直流升压电路,它通过开关管导通和关断来控制电感储存和释放能量,从而使输出电压比输入电压高。现在的开关电源一般是由脉冲宽度调制(PWM)控制IC和MOSFET构成,结合各种开关电源拓扑结构,组成完整的开关电源,开关电源最主要的是开关IC,如下图是BOOST升压电路拓扑结构,主要是由电感L1...
文章目录1、0-1背包问题2、旅行售货员问题3、图的m着色问题一、实验目的:掌握用回溯法解题的算法框架;根据回溯法解决实际问题。二、实验所用仪器及环境Windows 7 以上操作系统,PC机,codeblocks环境三、实验原理:算法总体思想:回溯法的基本做法是搜索,或是一种组织得井井有条的,能避免不必要搜索的穷举式搜索法。这种方法适用于解一些组合数相当大的问题。回溯法在问题的解空间树中,按深度优先策略,从根结点出发搜索解空间树。算法搜索至解空间树的任意一点时,先判断该结点是否包含问题的解。如果
转载:https://www.cnblogs.com/ooon/p/5711516.html理解 Bias 与 Variance 之间的权衡有监督学习中,预测误差的来源主要有两部分,分别为 bias 与 variance,模型的性能取决于 bias 与 variance 的 tradeoff ,理解 bias 与 variance 有助于我们诊断模型的错误,避免 over-fit...
1.在QT创建的工程中,右击鼠标,弹出菜单栏,点击创建新文件2.选择QT,选中QT resoure file ,点击choose3.创建名字4.我这里创建的名字是picture1,点击继续,则出现以下界面,点击完成即可5.创建完成后,则出现如下界面,可看到一个qrc的文件6.右击鼠标,在右键菜单中,选中Open in Editor7.进入编辑界面,可...
源自/网络近日,有网友在网络上晒出了一张深圳普通中学老师的工资单,秒杀互联网的程序员们,引起大家的讨论。这个帖子一出,尤其是一想到对方还有寒暑假,让不少程序员羡慕不已:时薪不是一般高啊。...