qml-使用 listView 构筑三级树形(treeView)架构

qml-使用 listView 构筑三级树形(treeView)架构

感谢这两位大佬:
https://blog.csdn.net/luoyayun361/article/details/81585101
https://blog.csdn.net/gongjianbo1992/article/details/90575519

核心

一级架构:直接使用listElement
二级架构:使用孩子元素,其中孩子元素为键值对;用repeater 来调用孩子元素
三级架构:不使用repeater ,而是再次使用listView;再展示其孩子,即第三代。

不足

布局不好看,大伙自己调一调。

效果展示

在这里插入图片描述

代码如下

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Window 2.2
import QtQuick.Layouts 1.1

Item
{
    id: simulation_edit_control_item_id

    ListModel {
        id: myModel
        Component.onCompleted:
        {
             function findIndex(fatherName)
             {
                for(var i = 0 ; i < myModel.count ; ++i)
                {
                    if(myModel.get(i).name === fatherName)
                    {
                        return i
                    }

                }
                return -1
             }

             function addModelData(fatherName,picture_source,name)
             {
                var index = findIndex(fatherName)
                 //console.log("fatherName:...",index)
                if(index === -1)
                {
                    //如果不存在,则它自己作为新的父节点
                    myModel.append({"name":fatherName,"picture_source":picture_source,"level":0,"thSubNode":[]})
                }
             }

             var listMenu=[ "四轮车", "二轮车" , "人" ]
             var source=""

             //创建父列表
             for(var i=0;i<3;i++)
             {
                 source=""
                 addModelData(listMenu[i],source,"" );
             }

             myModel.get(0).thSubNode.append({"name":"小汽车","picture_source":"","level":1,"subNode":[]})
             myModel.get(1).thSubNode.append({"name":"摩托车","picture_source":"","level":1,"subNode":[]})
             myModel.get(1).thSubNode.append({"name":"自行车","picture_source":"","level":1,"subNode":[]})
             myModel.get(2).thSubNode.append({"name":"行人","picture_source":"","level":1,"subNode":[]})

            myModel.get(2).thSubNode.get(0).subNode.append({"name":"行人行人","picture_source":"","level":2})
            myModel.get(1).thSubNode.get(0).subNode.append({"name":"自行车零件","picture_source":"","level":2})

             //console.log("hello",myModel.get(1).subNode.get(0).thSubNode.name)
        }
    }

    Component
    {
        id:list_delegate

        Column
        {
            id:objColumn
            leftPadding: 20
            Component.onCompleted:
            {
                //默认展开列表
                for(var i = 1; i < objColumn.children.length - 1; ++i)
                {
                    objColumn.children[i].visible = true
                }
            }
            Row
            {
                spacing:10
                leftPadding: 10
                Rectangle
                {
                    id:list_rect
                    width:220
                    height:30
                    color:"#EDF1F2"

                    Image
                    {
                        id: icon
                        anchors.left: parent.left
                        anchors.top: parent.top
                        anchors.topMargin:  6
                        anchors.leftMargin: 3
                        width: 20
                        height: 20
                        source: picture_source  //来自myModel的一级列表图标路径属性
                    }

                    Text
                    {
                        id: list_name
                        x:icon.x + icon.width +11
                        y: icon.y
                        width: 24
                        height: 19
                        text: name  //来自myModel的一级列表名字属性
                        font.styleName: "Regular"
                        font.pixelSize: 12
                        color: "#505559"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                    }
                }


            }
            Item
            {
                width: 220
                height:180
                //border.color: "blue"
                //需要加个item来撑开,如果用Repeater获取不到count
                ListView
                {
                    id: item_repeater
                    anchors.fill: parent
                    anchors.top: parent.top
                    anchors.topMargin:5
                    //spacing: 20

                    orientation: ListView.Vertical  //垂直列表
                    interactive: true;              //元素可用鼠标滚轮
                    clip: true                      //限制顶部,使得不超出区域
                    boundsBehavior:Flickable.StopAtBounds  //listview不跳动

                    delegate: subNode_delegate
                    model: thSubNode
                }
            }
        }

    }
    Component
    {
        id:subNode_delegate

        //重复排布myModel的子节点subNode
        Column
        {
            leftPadding:10
            Rectangle
            {     //子物体展示,如车辆、行人的图片、文字信息。
                id:role_rect_id
                x:0
                y:0
                width: 210
                height: 40
                border.color:"grey"
                Image
                {
                    id: role_icon
                    anchors.left: parent.left
                    anchors.top: parent.top
                    anchors.topMargin:  8
                    anchors.leftMargin: 28
                    width: 50
                    height: 50
                    source: picture_source
                    fillMode: Image.PreserveAspectFit
                }
                Text
                {
                    id: list_name_id
                    x:role_icon.x + role_icon.width + 9
                    y:role_icon.y
                    width: 65
                    height: 19
                    text: name
                    font.styleName: "Regular"
                    font.pixelSize: 12
                    color: "#505559"
                    horizontalAlignment: Text.AlignLeft
                    verticalAlignment: Text.AlignVCenter
                }
           }
            Repeater
            {
                model:subNode
                delegate:
                    Rectangle
                    {
                        id:role_rect
                        x:20
                        y:0
                        width: 190
                        height: 70
                        border.color:"green"
                        Image
                        {
                            id: icon
                            anchors.left: parent.left
                            anchors.top: parent.top
                            anchors.topMargin:  8
                            anchors.leftMargin: 28
                            width: 50
                            height: 50
                            source: picture_source
                            fillMode: Image.PreserveAspectFit
                        }
                        Text
                        {
                            id: list_name
                            x:role_icon.x + role_icon.width + 9
                            y:role_icon.y
                            anchors.top: parent.top
                            anchors.topMargin:  24
                            width: 65
                            height: 19
                            text: name
                            font.styleName: "Regular"
                            font.pixelSize: 12
                            color: "#505559"
                            horizontalAlignment: Text.AlignLeft
                            verticalAlignment: Text.AlignVCenter
                        }
                   }
            }
        }
    }

    ListView
    {
        id:listView
        anchors.fill: parent
        anchors.top: parent.top
        anchors.topMargin:20
        spacing: 20

        orientation: ListView.Vertical  //垂直列表
        interactive: true;              //元素可用鼠标滚轮
        clip: true                      //限制顶部,使得不超出区域
        boundsBehavior:Flickable.StopAtBounds  //listview不跳动

        model:myModel
        delegate: list_delegate
    }
    MouseArea
    {
        x:0
        y:0
        width:0
        height:0
//        anchors.fill:parent
//        hoverEnabled: true
//        propagateComposedEvents: true
//        onClicked:
//        {
//            mouse.accepted =false
//        }
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ListViewQML 中的一个常用组件,用于展示一个可滚动的列表。你可以通过以下步骤来使用 ListView: 1. 在 QML 文件中导入 ListView 组件: ```qml import QtQuick.Controls 2.5 ``` 2. 在 ListView 中添加一个模型(model),这个模型可以是一个数组或者是一个代表数据的对象: ```qml ListView { width: 200 height: 400 model: ["Apple", "Banana", "Cherry", "Date"] } ``` 3. 在 ListView 中定义一个委托(delegate),用于展示列表中的每个项: ```qml ListView { width: 200 height: 400 model: ["Apple", "Banana", "Cherry", "Date"] delegate: Rectangle { width: 200 height: 40 border.color: "black" Text { text: modelData } } } ``` 在上面的例子中,我们定义了一个矩形作为委托,并在矩形中添加了一个文本框,用于显示列表中的每个项。 4. (可选)对 ListView 进行更多的自定义设置,比如设置滚动条的样式、设置每个项的高度等等。 ```qml ListView { width: 200 height: 400 model: ["Apple", "Banana", "Cherry", "Date"] delegate: Rectangle { width: 200 height: 40 border.color: "black" Text { text: modelData } } spacing: 10 // 设置项之间的间隔 clip: true // 设置溢出部分是否被剪裁 highlight: Rectangle { color: "lightgray" } // 设置选中项的高亮颜色 scrollBar.vertical: ScrollBar { } // 设置垂直滚动条的样式 snapMode: ListView.SnapToItem // 设置滚动到最接近的项 } ``` 以上就是使用 ListView 的基本步骤,你可以根据自己的需求进行更多的自定义设置。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值