niepangu 2021-02-28 23:36 采纳率: 50%
浏览 880
已结题

QML 中如何更新Model的数据

我写了一个小程序,实现页面循环的滑动:

PetsModel.qml文件:

import QtQuick 2.0
ListModel {

 
    ListElement {
        name: "Polly"
        type: "Parrot"
        age: 12
        size: "Small"
    }
    ListElement {
        name: "Penny"
        type: "Turtle"
        age: 4
        size: "Small"
    }
}

 


HightRange.qml文件:

import QtQuick 2.0

 
Rectangle {
    id: root
    property int current: 0
    property bool increasing: true
    property int count: 0

 
    PathView {
        id: view
        anchors.fill: parent
        snapMode: PathView.SnapOneItem
        highlightRangeMode: PathView.StrictlyEnforceRange
        currentIndex: -1
        model: PetsModel {id: aModel}

 
        delegate: petDelegate
        path: Path {

 
            startX: -view.width / 2 // let the first item in left
            startY: view.height / 2 // item's vertical center is the same as line's
            PathLine {
                relativeX: view.width * view.model.count // all items in lines
                relativeY: 0
            }
        }

 
    }

 
    Component {
        id: petDelegate
        Item {
            width: 480
            height: 800

 
            Rectangle{
                anchors.fill: parent
                Text {
                    id: first
                    width: parent.width
                    height: parent.height / 3
                    anchors.top:parent.top
                    text: name
                }
                Text {
                    id: second
                    anchors.top:first.bottom
                    width: parent.width
                    height: parent.height / 3
                    text: type
                }
                Text {
                    id: third
                    anchors.top:second.bottom
                    width: parent.width
                    height: parent.height / 3
                    text: age
                }
            }

 
            MouseArea {
                anchors.fill: parent
                onClicked: root.current = index
            }
        }
    }
}

 


main.qml文件:

import QtQuick 2.9
import QtQuick.Window 2.2

 
Window {
    visible: true
    width: 480
    height: 800
    title: qsTr("Hello World")
    HightRange {
        anchors.fill:parent
    }
}
 

这样出来的界面上显示的信息是固定的,无法改变,我想实现界面上的信息定时改变该怎么做?例如在不滑动界面的情况下每秒age加一

 

  • 写回答

1条回答 默认 最新

  • 庐州李大爷 2021-03-30 14:17
    关注
        Timer {
            interval: 1000; running: true; repeat: true
            onTriggered: {
                console.log("timer is out : 1s :: ");
                var i = aModel.get(0).age;
                i++;
                aModel.get(0).age = i;
            }
        }

    在HighRange.qml的最后面加上这一段代码即可。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 4月27日
  • 已采纳回答 4月19日

悬赏问题

  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因