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日

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?