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
                    }
                }
            }
        }
    }
}

演示

视频讲解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值