Qt/QML学习-ListView

QML学习

在这里插入图片描述

main.qml

import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Controls 2.15

Window {
    id: window
    width: 640
    height: 480
    visible: true
    title: qsTr("ListView")

    Rectangle {
        height: listView.height
        width: listView.width
        color: "blue"
        clip: true
        ListView {
            id: listView
            width: 250
            height: window.height
            model: ListModel {
                ListElement {
                    name: "第一组"
                    subListElement:[
                        ListElement {content: "ListView"},
                        ListElement {content: "放假当宅宅"},
                        ListElement {content: "点赞" }
                    ]
                }
                ListElement {
                    name: "第二组"
                    subListElement:[
                        ListElement {content: "ListView"},
                        ListElement {content: "放假当宅宅"},
                        ListElement {content: "点赞" },
                        ListElement {content: "投币"}
                    ]
                }
                ListElement {
                    name: "第三组"
                    subListElement:[
                        ListElement {content: "ListView"},
                        ListElement {content: "放假当宅宅"},
                        ListElement {content: "点赞" },
                        ListElement {content: "投币"},
                        ListElement {content: "关注"}
                    ]
                }
            }
            delegate: ChildListView {
                title: name
                childModel: subListElement
            }
            ScrollBar.vertical: ScrollBar {
                background: Rectangle {
                    color: "#ffffff"
                }
                interactive: true
            }
        }
    }
}

ChildListView.qml

import QtQuick 2.15

Rectangle {
    width: listView.width
    height: listView.height + rectIcon.height
    color: "white"
    property int iconSize: 50
    property int viewSize: 200
    property var title
    property var childModel

    Rectangle {
        id: rectIcon
        width: iconSize
        height: iconSize
        border.color: "white"
        color: "red"
    }
    Rectangle {
        id: rect
        width: viewSize
        height: iconSize
        anchors.left: rectIcon.right
        border.color: "white"
        color: "black"
        Text {
            anchors.centerIn: parent
            color: "white"
            text: title
            font{
                pointSize: 20
                bold: true
            }
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                listView.height = listRect.height == 0? (listView.count * 50): 0
                listRect.height = listRect.height == 0? listView.height: 0
            }
        }
    }
    Rectangle {
        id: listRect
        height: listView.height
        width: listView.width
        anchors.left: rectIcon.right
        anchors.top: rect.bottom
        color: "blue"
        ListView {
            id: listView
            width: viewSize
            height: count * (viewSize / 4)
            clip: true
            model: childModel
            delegate: Rectangle {
                width: listView.width
                height: (viewSize / 4)
                color: index % 2 === 0 ? "#30ffaf" : "#f6ff08"
                Text {
                    anchors.centerIn: parent
                    text: content;
                    font{
                        pointSize: 20
                        bold: true
                    }
                }
            }
        }
    }
}

演示

视频讲解

相关推荐

  1. ListView

    2024-07-10 12:14:05       47 阅读
  2. ListView

    2024-07-10 12:14:05       23 阅读
  3. <span style='color:red;'>ListView</span>

    ListView

    2024-07-10 12:14:05      12 阅读
  4. <span style='color:red;'>ListView</span>

    ListView

    2024-07-10 12:14:05      8 阅读
  5. ListView[new]

    2024-07-10 12:14:05       16 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 12:14:05       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 12:14:05       5 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 12:14:05       4 阅读
  4. Python语言-面向对象

    2024-07-10 12:14:05       5 阅读

热门阅读

  1. 如何减少开发过程中的bug-数据库篇

    2024-07-10 12:14:05       9 阅读
  2. 驻场运维的前途在哪里,这里有金玉良言

    2024-07-10 12:14:05       9 阅读
  3. 认字之 刬

    2024-07-10 12:14:05       7 阅读
  4. lvs集群

    lvs集群

    2024-07-10 12:14:05      9 阅读
  5. AUTOSAR:汽车软件架构的未来

    2024-07-10 12:14:05       9 阅读
  6. 相机光学(二十九)——显色指数(Ra)

    2024-07-10 12:14:05       8 阅读
  7. 贪心算法合集一

    2024-07-10 12:14:05       8 阅读
  8. 索引知识总结

    2024-07-10 12:14:05       10 阅读
  9. Oracle怎么实现RSA加密解密

    2024-07-10 12:14:05       8 阅读