本页内容

CalendarModel QML Type

日历模型。更多...

Import Statement: import QtQuick.Controls

属性

方法

详细说明

CalendarModel 提供了一种创建一系列MonthGrid 实例的方法。它通常用作ListView 的模型,该控件使用MonthGrid 作为委托。

ListView {
    id: listview

    width: 200; height: 200
    snapMode: ListView.SnapOneItem
    orientation: ListView.Horizontal
    highlightRangeMode: ListView.StrictlyEnforceRange

    model: CalendarModel {
        from: new Date(2015, 0, 1)
        to: new Date(2015, 11, 31)
    }

    delegate: MonthGrid {
        width: listview.width
        height: listview.height

        month: model.month
        year: model.year
        locale: Qt.locale("en_US")
    }

    ScrollIndicator.horizontal: ScrollIndicator { }
}

除了 `index ` 属性外,每个委托的上下文中还提供了一组模型数据角色:

model.month: int月份编号
model.year: int年份

日历相关的类型在 Qt Quick Controls 使用以0为起点的月份编号,与JavaScript的Date对象类似。但这与QDate 不同,后者中的月份编号是从1开始的。

为避免混淆,您可以使用以下枚举值:

常量描述
Calendar.January一月 (0)
Calendar.February二月 (1)
Calendar.March3月 (2)
Calendar.April4月 (3)
Calendar.May5月 (4)
Calendar.June6月 (5)
Calendar.July7月 (6)
Calendar.August8月 (7)
Calendar.September9月 (8)
Calendar.October10月 (9)
Calendar.November11月 (10)
Calendar.December12月 (11)

另请参阅 MonthGrid 和Calendar 。

属性文档

from : date

该属性保存开始日期。

to : date

该属性存储结束日期。

方法文档

int indexOf(Date date)

返回指定date 的模型索引。

int indexOf(int year, int month)

返回指定year 和month 的模型索引。

int monthAt(int index)

返回指定模型index 中的月份编号。

int yearAt(int index)

返回指定模型index 对应的年份编号。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.