QML 基本模型(二)

前言

Repeater元素适合有限的静态数据,但是在真正使用时,模型通常更加复杂和庞大,我们需要一个更加智能的解决方案。QtQuick提供了ListView和GridView元素,这两个都是基于Flickable(可滑动)区域的元素,因此用户可以放入更大的数据。同时,它们限制了同时实例化的代理数量。对于一个大型的模型,这意味着在同一个场景下只会加载有限的元素。

import QtQuick 2.14
import QtQuick.Controls 2.14



ApplicationWindow {
    visible: true
    width: 400
    height: 600
    title: qsTr("Tabs")

    Rectangle {
        anchors.fill: parent
        color: "white"
        ListModel {
            id: theModel
            ListElement { number: 0 }
            ListElement { number: 1 }
        Rectangle {
            anchors.left: parent.left
            anchors.right: parent.right
            anchors.bottom: parent.bottom
            anchors.margins: 50
            height: 40
            color: "darkGreen"
            Text {
                anchors.centerIn: parent
                text: "Add item!"
            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    theModel.append({"number": ++parent.count});
                }
            }

            property int count: 2
        }

        ListView {
            anchors.fill: parent
            anchors.margins: 20
            anchors.bottomMargin: 80
            clip: true
            model: theModel
            width: 45
            height: 45
            delegate: numberDelegate
        }

        Component {
            id: numberDelegate
            Rectangle {
                id: wrapper
                width: 45
                height: 45
                color: "lightGreen"
                Text {
                    anchors.centerIn: parent
                    font.pixelSize: 10
                    text: number
                }
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        if (!wrapper.ListView.delayRemove)
                            theModel.remove(index);
                    }
                }

                ListView.onRemove: SequentialAnimation {
                    PropertyAction { target: wrapper; property: "ListView.delayRemove"; value: true }
                    NumberAnimation { target: wrapper; property: "scale"; to: 0; duration: 250; easing.type: Easing.InOutQuad }
                    PropertyAction { target: wrapper; property: "ListView.delayRemove"; value: false }
                }

                ListView.onAdd: SequentialAnimation {
                    NumberAnimation { target: wrapper; property: "scale"; from: 0; to: 1; duration: 250; easing.type: Easing.InOutQuad }
                }
            }
        }
    }

}

NumberAnimation : 属于动画功能
NumberAnimation {
target: wrapper; 绑定的对象
property: “scale”; 动画的属性, 缩放
from: 0; 开始
to: 1; 结束
duration: 250; 动画时间 250毫秒
easing.type: Easing.InOutQuad 释放类型。
}

参考

https://cwc1987.gitbooks.io/qmlbook-in-chinese/content/model-view-delegate/dynamic_views.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值