1. 程式人生 > >QML自定義樹控制元件(TreeView 的style加上節點可拖動)

QML自定義樹控制元件(TreeView 的style加上節點可拖動)

  • 背景:

         前段時間工作需要使用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")
                            }
                         }
                  }
    }
}