QML自定义树控件(TreeView 的style加上节点可拖动)_闻天语~的博客-程序员秘密

技术标签: QML  Drag  DropArea  TreeView  

  • 背景:

         前段时间工作需要使用QML的TreeView,要通过拖动节点,对应节点执行对应的操作,查了很多的资料,没有看到关于节点可拖动的资料,查看TreeView的源码,貌似存在关于节点拖动的地方,但是始终没有看到可以使用的接口,只好自己动手造轮子了,加上使用过程中踩了不少的坑,也算是给后面用到的人一个案例吧,如果有什么好的建议,什么疑问,又或者有好的萝卜坑推荐,都可以加这个QQ:995187021,即使没有解决问题,也可以交个朋友。

  • 效果:

  • 代码:(直接上代码了,不知道怎么描述,看注释吧,有问题加QQ)

MyTreeView.qml

 

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" //这里决定了节点选中的颜色和背景颜色
            }
            
        }
    }
}

main.qml

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

 

 

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_40912639/article/details/83962250

智能推荐

ramdisk.img system.img userdata.img boot.img分析_zhouxiangbai的博客-程序员秘密

ramdisk.img system.img userdata.img 分析: http://blog.csdn.net/wuye110/article/details/8463820

Git checkout 指令总结:切换本地和远程分支、查看本地与远程分支关系_checkout 远程分支_在奋斗的大道的博客-程序员秘密

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

Bootstrap 3 排版_weixin_33701294的博客-程序员秘密

2019独角兽企业重金招聘Python工程师标准&gt;&gt;&gt; ...

ffmpeg编解码详细过程_ffmpeg map_wishfly的博客-程序员秘密

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 , sigpending 和 sigsuspend函数_elbort的博客-程序员秘密

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

camera-link相机模拟器:仿真相机生成图片数据流_虹科FPGA的博客-程序员秘密

CamSim是一个灵活的高性能相机模拟器,可为图像采集卡生成Cameral Link或CoaXPress视频流和测试图案。该系统支持所有Camera Link规范v2.0配置、CoaXPress标准规范1.1版和任何所需的用户接口。该相机模拟器可以实现在一个低成本的安静的实验室环境中进行大部分机器视觉的项目开发。因此,CamSim可以极大地提高生产力,降低了开发视觉和成像系统的总体费用。同时CamSim数据流重复能力确保算法得到验证,并在相关的输入下按预期工作。此外,一旦检测到一个罕见的错误,其各自

随便推点

ubuntu64位下安装libjpeg.so.62_libjpeg.so.62安装_Rexxxxxxxxxx的博客-程序员秘密

安霸平台编译过程中提示: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升压电路图_爱学习的王大胖子的博客-程序员秘密

boost升压电路(boost converter or step-up converter)是一种常见的开关直流升压电路,它通过开关管导通和关断来控制电感储存和释放能量,从而使输出电压比输入电压高。现在的开关电源一般是由脉冲宽度调制(PWM)控制IC和MOSFET构成,结合各种开关电源拓扑结构,组成完整的开关电源,开关电源最主要的是开关IC,如下图是BOOST升压电路拓扑结构,主要是由电感L1...

算法分析与设计-实验四 回溯算法设计_新川宝宝的博客-程序员秘密

文章目录1、0-1背包问题2、旅行售货员问题3、图的m着色问题一、实验目的:掌握用回溯法解题的算法框架;根据回溯法解决实际问题。二、实验所用仪器及环境Windows 7 以上操作系统,PC机,codeblocks环境三、实验原理:算法总体思想:回溯法的基本做法是搜索,或是一种组织得井井有条的,能避免不必要搜索的穷举式搜索法。这种方法适用于解一些组合数相当大的问题。回溯法在问题的解空间树中,按深度优先策略,从根结点出发搜索解空间树。算法搜索至解空间树的任意一点时,先判断该结点是否包含问题的解。如果

理解 Bias 与 Variance 之间的权衡_bias和variance的权衡_沉默的大多数的杂谈感想的博客-程序员秘密

转载:https://www.cnblogs.com/ooon/p/5711516.html理解 Bias 与 Variance 之间的权衡有监督学习中,预测误差的来源主要有两部分,分别为 bias 与 variance,模型的性能取决于 bias 与 variance 的 tradeoff ,理解 bias 与 variance 有助于我们诊断模型的错误,避免 over-fit...

QT creator:创建和添加资源resource_qtcreator resource_我爱吃辣椒@的博客-程序员秘密

1.在QT创建的工程中,右击鼠标,弹出菜单栏,点击创建新文件2.选择QT,选中QT resoure file ,点击choose3.创建名字4.我这里创建的名字是picture1,点击继续,则出现以下界面,点击完成即可5.创建完成后,则出现如下界面,可看到一个qrc的文件6.右击鼠标,在右键菜单中,选中Open in Editor7.进入编辑界面,可...

深圳一普通中学老师工资单曝光,秒杀程序员,网友:酸了酸了_小詹学 Python的博客-程序员秘密

源自/网络近日,有网友在网络上晒出了一张深圳普通中学老师的工资单,秒杀互联网的程序员们,引起大家的讨论。这个帖子一出,尤其是一想到对方还有寒暑假,让不少程序员羡慕不已:时薪不是一般高啊。...

推荐文章

热门文章

相关标签