wang12zhe 发表于 2021-3-1 13:01:27

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]
查看完整版本: QML 中如何更新Model的数据