Qml 日历组件要疯了吗?

Is Qml Calendar component going crazy?

在给Qt项目开bug之前,我想问你是我做错了什么还是Qml Calendar真的要疯了。

它遵循可用于测试它的代码:

import QtQuick 2.5
import QtQuick.Window 2.0
import QtQuick.Controls 1.4
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.1

Window {
    visible: true
    width: 1024
    height: 768

    ColumnLayout {
        anchors.fill: parent
        anchors.margins: 8

        Calendar {
            id: calendar
            Layout.fillWidth: true
            Layout.fillHeight: true
            weekNumbersVisible: true
            selectedDate: new Date("2015/01/01")
            frameVisible: true
            focus: true

            onVisibleMonthChanged: visibleMonthChangedRef.currDate = visibleYear+"/"+visibleMonth
            onVisibleYearChanged: visibleYearChangedRef.currDate = visibleYear+"/"+visibleMonth
        }

        Label {
            id: visibleMonthChangedRef
            Layout.fillWidth: true
            property string currDate: ""
            text: "onVisibleMonthChanged -> " + currDate
            Component.onCompleted: font.pointSize = font.pointSize*2
        }

        Label {
            id: visibleYearChangedRef
            Layout.fillWidth: true
            property string currDate: ""
            text: "onVisibleYearChanged -> " + currDate
            Component.onCompleted: font.pointSize = font.pointSize*2
        }
    }
}

轻松,启动应用程序,您将看到一个日历以及几个标签,这些标签报告 visibleMonthvisibleYear 提供的信息 Calendar 零件。 这些标签根据 CalendaronVisibleYearChangedonVisibleMonthChanged 进行填充。 所选日期为 2015/01/01.

好吧,倒退一个月到 2014 年。

onVisibleYearChanged 在访问 CalendarvisibleMonthvisibleYear 属性时看起来是正确的,而 onVisibleMonthChanged 在我看来是活的在遥远的未来。

现在,试着向 2015 年迈进一个月。

同样,虽然 onVisibleYearChanged 仍然以正确的方式运行,但 onVisibleMonthChanged 正在访问一个位于过去的 Calendar 组件。

你可以在新的一年里来来回回,一个对信号做出反应的函数 onVisibleMonthChanged 将永远不会正常运行,因为一旦所有内部属性都没有正确设置,它看起来就像被调用一样还。

也就是说,我是不是做错了什么,因此问题出在我的代码中(当然,上面的代码是一个更复杂项目中的一个小例子),或者我真的在“日历”中发现了一个错误` 组件,我应该继续为 Qt 项目开票吗?

虽然我要求修改一个组件看起来有点奇怪,但被告知它已更新,因此发现我的更改只是部分设置(一种正在进行的通知),仅此而已。

看来错误在我的意料之中。 :-)

一旦 monthyear 上移动或返回(即从 12 月到 1 月,反之亦然),Calendar 组件会正确更新 property visibleMonthproperty visibleYear.

无论如何,一旦内部表示作为一个整体更新,就不能保证发出相应的信号(onVisibleMonthChangedonVisibleYearChanged)。

实际上,前者是在对第二个 属性 的更改仍未决时发出的,因此在侦听器中查询 visibleYear 属性 以 结束错误 年(或至少,一个尚未更新的年份)。

作为解决方案的示例,组件的用户可以通过将侦听器附加到两个信号来解决这种奇怪的行为,尽管我强烈怀疑外面有更好的解决方案在等着我。

我会设法找到替代方法。 感谢您的所有回复。