通过模型迭代的QML

通过模型迭代的QML,qml,qt5,qtquick2,Qml,Qt5,Qtquick2,我试图在QML中将ListModel的内容绘制到画布上。该模型的内容显示在应用程序其他位置的列表视图中,因此我知道该模型正确地填充了内容 否每次模型数据更改时,我都尝试更新画布: import QtQuick 2.7 import QtQml.Models 2.2 Item { Canvas { anchors.fill: parent id: canvas onPaint: { console.log("onP

我试图在QML中将ListModel的内容绘制到画布上。该模型的内容显示在应用程序其他位置的列表视图中,因此我知道该模型正确地填充了内容

否每次模型数据更改时,我都尝试更新画布:

import QtQuick 2.7
import QtQml.Models 2.2

Item {
    Canvas {
        anchors.fill: parent
        id: canvas

        onPaint: {
            console.log("onPaint()")
            var ctx = getContext("2d")
            ctx.fillStyle = Qt.rgba(0, 0, 0, 1)
            ctx.fillRect(0, 0, width, height)

            console.log(particleListModel.count)
            for(var i = 0; i < particleListModel.count; i++) {
                console.log(i)
            }
        }
    }

    Connections {
        target: particleListModel

        onDataChanged: {
            console.log("data changed")
            canvas.requestPaint()
        }
    }
}
没有定义


当常规ListView能够正确显示内容时,这怎么可能呢?

在使用模型时,您需要调用rowCount函数,而不是count函数,因为后者是ListView的属性,而不是模型的属性

以下方面应起作用:

console.log(particleListModel.rowCount())

你不应该用rowCount代替count吗?谢谢!将rowCount作为函数调用解决了这个问题。
console.log(particleListModel.rowCount())