QML ListView

QML ListView

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4 as Ctr_1_4
import QtQuick.Layouts 1.15
import "./common.js"  as FunJs

Window {
    id: window
    width: 600
    height: 480
    visible: true
    title: "QML Demo"

    Item {
        id: rootItem
        anchors.fill: parent
        property var colorBuilder: FunJs.getColorBudider(Qt.rgba)

        ListView{
            id:rootView
            anchors.fill:parent
            model:["data1","data2","data3","data4","data5","data6"]
            delegate: ItemDelegate{
                width: rootItem.width
                text: modelData

                background: Rectangle{
                     color: rootItem.colorBuilder()
                }

                onClicked: {
                    console.log(modelData)
                }
            }

            //滚动条指示器 不可拖动
            //ScrollIndicator.vertical: ScrollIndicator{}

            //滚动条 可拖动
            ScrollBar.vertical: ScrollBar{}

        }
    }
}
  • common.js
function getColorRangeRandom(max){
    return Math.floor(Math.random() * max)
}


function  getColorRandom(){
    let red = getColorRangeRandom(256)
    let green = getColorRangeRandom(256)
    let blue = getColorRangeRandom(256)
    return {red: red / 255, green: green / 255, blue: blue / 255}
}


function getColorBudider(rgbFunction){
    return function(){
        let color = getColorRandom()
        return rgbFunction(color.red, color.green, color.blue)
    }
}
  • 显示:

示例2

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4 as Ctr_1_4
import QtQuick.Layouts 1.15
import "./common.js"  as FunJs

Window {
    id: window
    width: 600
    height: 480
    visible: true
    title: "QML Demo"

    Item {
        id: rootItem
        anchors.fill: parent
        property var colorBuilder: FunJs.getColorBudider(Qt.rgba)

        ListView{
            id:rootView
            anchors.fill:parent
            model:[
                {name: "Lucy", age: "12"},
                {name: "Peter", age: "9"},
                {name: "Ben", age: "16"}
            ]
            delegate: ItemDelegate{
                width: rootItem.width
                text: "name:" +  modelData.name + "age:" +  modelData.age + (rootView.currentIndex === index ? '√' : "")

                background: Rectangle{
                    color: rootItem.colorBuilder()
                }

                onClicked: {
                    console.log(modelData)
                    rootView.currentIndex = index
                }
            }

            //滚动条指示器 不可拖动
            //ScrollIndicator.vertical: ScrollIndicator{}

            //滚动条 可拖动
            ScrollBar.vertical: ScrollBar{}
        }
    }
}
  • 输出:

ListMdel

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4 as Ctr_1_4
import QtQuick.Layouts 1.15
import "./common.js"  as FunJs

Window {
    id: window
    width: 600
    height: 480
    visible: true
    title: "QML Demo"

    Item {
        id: rootItem
        anchors.fill: parent
        property var colorBuilder: FunJs.getColorBudider(Qt.rgba)

        ListView{
            id:rootView
            anchors.fill:parent
            model:ListModel{
                id: listModel
                ListElement {
                    name: "Lucy"
                    age: "12"
                }
                ListElement {
                    name: "Peter"
                    age: "9"
                }
                ListElement {
                    name: "Ben"
                    age: "16"
                }
            }

            delegate: Rectangle{
                width: rootView.width
                height: rootView.height * 0.1
                color: rootItem.colorBuilder()
                Text{
                    //如果要文字居中设置,则Text的父对象必须显示设置宽高,不要使用锚点设置
                    anchors.fill: parent
                    text: "name:" + name + "  age :" + age + (parent.isCurrentIndex() ? "√" : "")
                    font.pointSize: 15
                    verticalAlignment: Text.AlignVCenter
                    horizontalAlignment: Text.AlignHCenter
                }

                MouseArea{
                    anchors.fill: parent
                    onClicked: {
                        let data = listModel.get(index)
                        console.log(JSON.stringify(data))
                        setCurrentIndex()
                    }
                }

                function isCurrentIndex(){
                    return rootView.currentIndex === index
                }

                function setCurrentIndex(){
                    rootView.currentIndex = index
                }

            }

            Component.onCompleted: {
                listModel.append({ name: "Ben",age: "16"})
            }


            //滚动条 可拖动
            ScrollBar.vertical: ScrollBar{}

        }
    }
}
  • 显示:

Section

  • 使用分组,模型必须是ListModel
  • 示例
import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4 as Ctr_1_4
import QtQuick.Layouts 1.15
import "./common.js"  as FunJs

Window {
    id: window
    width: 600
    height: 480
    visible: true
    title: "QML Demo"

    Item {
        id: rootItem
        anchors.fill: parent
        property var colorBuilder: FunJs.getColorBudider(Qt.rgba)

        ListView{
            id:rootView
            anchors.fill: parent

            model:ListModel{
                ListElement{
                    name:"Alice"
                    group: "student"
                }
                ListElement{
                    name:"Ben"
                    group: "student"
                }
                ListElement{
                    name:"Cudy"
                    group: "student"
                }

                ListElement{
                    name:"Deni"
                    group: "student"
                }
                ListElement{
                    name:"Ella"
                    group: "teacher"
                }
                ListElement{
                    name:"Fily"
                    group: "teacher"
                }

            }


            delegate: Rectangle{
                width: parent.width
                height: 20
                color: rootItem.colorBuilder()
                required property string name

                Text{
                    anchors.centerIn: parent
                    text: name
                }
            }

            section.property: "group"
            section.delegate: Rectangle{
                width: parent.width
                height: 20
                color: rootItem.colorBuilder()
                required property string section

                Text {
                   anchors.centerIn: parent
                   text: section
                   font.bold: true
                   font.pointSize: 16
                }
            }
        }
    }
}
  • 输出:

附加属性和信号

import QtQuick 2.14
import QtQuick.Window 2.14
import QtQuick.Controls 2.5
import QtQuick.Controls 1.4 as Ctr_1_4
import QtQuick.Layouts 1.15
import "./common.js"  as FunJs

Window {
    id: window
    width: 600
    height: 480
    visible: true
    title: "QML Demo"

    Item {
        id: rootItem
        anchors.fill: parent
        property var colorBuilder: FunJs.getColorBudider(Qt.rgba)

        ListView{
            id:rootView
            anchors.fill: parent

            model:ListModel{
                ListElement{
                    name:"Alice"
                    group: "student"
                }
                ListElement{
                    name:"Ben"
                    group: "student"
                }
                ListElement{
                    name:"Cudy"
                    group: "student"
                }

                ListElement{
                    name:"Deni"
                    group: "student"
                }
                ListElement{
                    name:"Ella"
                    group: "teacher"
                }
                ListElement{
                    name:"Fily"
                    group: "teacher"
                }

            }


            delegate: Rectangle{
                width: ListView.view.width
                height: 20
                color: ListView.isCurrentItem ? rootItem.colorBuilder() : "gray"
                required property string name

                Text{
                    anchors.centerIn: parent
                    text: name
                    font.italic: parent.ListView.isCurrentItem
                }


            }

            section.property: "group"
            section.delegate: Rectangle{
                width: parent.width
                height: 20
                color: rootItem.colorBuilder()
                required property string section

                Text {
                   anchors.centerIn: parent
                   text: section
                   font.bold: true
                   font.pointSize: 16
                }
            }

            footer: Button{
                text: "append item"
                onClicked: {
                    rootView.model.append({name:"Zem", group: "teacher"})
                }
            }


        }
    }
}
  • 输出:

 

 

### QMLListView 的基本用法 `ListView` 是 QML 提供的一个强大组件,用于展示列表形式的数据。它可以绑定到多种类型的模型(如 `ListModel` 或者 C++ 定义的模型),并通过委托 (`delegate`) 来定义每一项的具体表现形式。 以下是关于如何使用 `ListView` 的详细说明以及示例代码: #### 基本结构 一个完整的 `ListView` 需要以下几个部分: 1. **model**: 数据源,可以是一个简单的数组、`ListModel` 实例或者更复杂的 C++ 模型。 2. **delegate**: 定义了每一条数据在视图中的具体呈现方式。 3. **orientation**: 可选参数,默认为垂直方向 (vertical),也可以设置为水平方向 (horizontal)[^1]。 下面提供了一个具体的例子来演示这些概念的实际应用。 ```qml import QtQuick 2.15 import QtQuick.Controls 2.15 ApplicationWindow { visible: true width: 400 height: 600 title: "QML ListView Example" ListModel { // 创建一个 ListModel 作为数据源 id: fruitModel ListElement { name: "Apple"; color: "red" } ListElement { name: "Banana"; color: "yellow" } ListElement { name: "Cherry"; color: "darkRed" } } Component { // 定义 delegate 组件 id: fruitDelegate Rectangle { width: parent.width; height: 40 color: index % 2 ? "#f8f8f8" : "#eaeaea" Text { anchors.centerIn: parent text: model.name + " (" + model.color + ")" font.pixelSize: 18 } } } ListView { // 主体 ListView 控件 anchors.fill: parent model: fruitModel delegate: fruitDelegate spacing: 2 ScrollBar.vertical: ScrollBar {} // 添加竖直滚动条 } } ``` 此段代码展示了如何利用 `ListModel` 和自定义 `Component` 构建一个基础的 `ListView`[^2]。其中包含了颜色交替的效果以增强视觉体验,并加入了滚动条以便于处理大量数据的情况。 #### 属性解释 - `anchors.fill`: 让 `ListView` 占满整个窗口区域。 - `spacing`: 设置相邻两项之间的间距大小。 - `ScrollBar.vertical`: 显式声明启用垂直滚动条功能。 ### 进阶特性 除了上述的基础操作外,还可以进一步扩展其能力,比如动态更新模型内容、响应用户交互事件等高级技巧。这里仅列举几个常用方法: - 修改现有项目的属性值; - 向模型中追加新项目; - 删除指定索引处的项目; 例如,在按钮点击时向模型添加新的水果名称: ```qml Button { text: "Add Fruit" onClicked: { var newItem = {"name": "Orange", "color": "orange"}; fruitModel.append(newItem); } } ``` 以上就是有关 QML 中 `ListView` 的一些基础知识及其简单实现案例介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值