Update/repaint 项手动

Update/repaint item manually

我的问题与 Calendar 有关,但可以应用于每个 QML 视觉对象 Item。有什么方法可以手动重绘 Item 吗?

在我的例子中,我有一个带有自定义内容的 Calendar(单元格 26 中的小橙色数字):

为此,我使用样式:

import QtQuick 2.5
import QtQuick.Window 2.2
import QtQuick.Controls 1.2
import QtQuick.Controls.Styles 1.2

Window {
    width: 300
    height: 300
    id: window
    Calendar {
        id: calendar
        anchors.fill: parent
        property var dataArr: {26: 7}
        style: CalendarStyle {
            dayDelegate: Rectangle {
                Label {
                    text: styleData.date.getDate()
                    anchors.centerIn: parent
                }
                Label {
                    font.pixelSize: 8
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    width: 12
                    height: 10
                    horizontalAlignment: Text.AlignHCenter
                    text: calendar.dataArr[styleData.date.getDate()] ? calendar.dataArr[styleData.date.getDate()] : ""
                    color: "orange"
                }
            }
        }
        Component.onCompleted: {
            calendar.dataArr[26] = 8; //that doesn't work
        }
    }
}

这适用于静态数组,但如果我更改数据数组中的值,它不会更新单元格。如何强制 Calendar 更新?

根据 this,不会以这种方式为 var 触发绑定更新。他们在示例之后提供了解决方案。在这里应用它:

property var dataArr: new Object( {26: 7} )
...
Component.onCompleted: {
   dataArr = new Object( {26: 8} )
}