QML --> ListModel(链表模型)的简单使用

一个链表模型(ListModel)是由许多个链表元素(ListElement)组成。

import QtQuick 2.0

Column{
    spacing: 2
    Repeater{
        model:ListModel{
            ListElement{name:"单机"; surfaceColor:"gray"}
            ListElement{name:"联机"; surfaceColor:"yellow"}
        }
        Rectangle{
            width: 100
            height: 20
            radius: 3

            color: "lightBlue"
            Text {
                anchors.centerIn: parent
                text: name
            }
            Rectangle{
                anchors.left: parent.left
                anchors.verticalCenter: parent.verticalCenter
                anchors.leftMargin: 2

                width: 16
                height: 16
                radius: 8
                border.color: "black"
                border.width: 1
                color: surfaceColor
            }
        }
    }
}

结果:

 

 

Column {
    spacing: 2
    Repeater {
        model: 10
        delegate: Rectangle {
            width: 100
            height: 20
            radius: 3
            color: "lightBlue"
            Text {
                anchors.centerIn: parent
                text: index
            }
        }
    }
}

 

posted on 2021-08-11 16:57  缘随风烬  阅读(1578)  评论(0编辑  收藏  举报