QML-ListView

//类似Repeater,批量生成组件
ListView{
        width: 180;height: 200
        model: ["Button","Rectangle","MouseArea"]
        //按钮间距
        spacing: 10
        delegate:Button{
            text: modelData
        }
    }
import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.5

Window {
    width: 640
    height: 480
    visible: true
    Rectangle{
        width: 500
        height: 500
        Component{
            id:sectionHeading
            Rectangle{
                width:container.width
                height: childrenRect.height
                color: "lightsteelblue"
                //控件的标题
                property string section: "标题123abc"
                Text {
                    text: parent.section
                    font.bold: true
                    font.pixelSize: 20
                }
            }
        }

        ListView{
            id:list
            width: 180
            height: 200
            //放到父控件中心
            anchors.centerIn:parent
            //头矩形
            header:Rectangle{
                width: 400
                height: 20
                color: "blue"
            }
            //尾矩形
            footer: Rectangle{
                width: 400
                height: 20
                color: "green"
            }

            model: ListModel{
                ListElement{
                    name:"Bill Smith"
                    number:"001"
                }
                ListElement{
                    name:"John Brown"
                    number:"002"
                }
                ListElement{
                    name:"Sam Wise"
                    number:"003"
                }
            }
            //数字 控制了所有的数据间隔
            spacing: 10
            //变为横向滑动
            flickableDirection:Flickable.HorizontalFlick

            //一个选中样式的背景
            highlight: Rectangle {
                color: "lightsteelblue"
                radius: 5
            }

            //控制了每一项数据是如何绘制的
            delegate:Rectangle{
                color:"transparent"
                width: list.width
                height: 50
                Text{
                    id: txt
                    text: name + " " + number
                }
                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        //点击后将highlight移动到点击位置
                        console.log(list.currentIndex = index)
                    }
                }
            }
            section.property: "number"
            section.criteria: ViewSection.FullString
            section.delegate: sectionHeading
        }
    }
}


 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值