QCalendarWidget 类

QCalendarWidget 类提供允许用户选择日期,基于月份的日历 Widget。 更多...

头: #include <QCalendarWidget>
CMake: find_package(Qt6 COMPONENTS Widgets REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Widgets)
qmake: QT += widgets
继承: QWidget

公共类型

enum HorizontalHeaderFormat { SingleLetterDayNames, ShortDayNames, LongDayNames, NoHorizontalHeader }
enum SelectionMode { NoSelection, SingleSelection }
enum VerticalHeaderFormat { ISOWeekNumbers, NoVerticalHeader }

特性

公共函数

  QCalendarWidget (QWidget * parent = nullptr)
virtual ~QCalendarWidget ()
int dateEditAcceptDelay () const
QMap<QDate, QTextCharFormat> dateTextFormat () const
QTextCharFormat dateTextFormat (QDate date ) const
Qt::DayOfWeek firstDayOfWeek () const
QTextCharFormat headerTextFormat () const
QCalendarWidget::HorizontalHeaderFormat horizontalHeaderFormat () const
bool isDateEditEnabled () const
bool isGridVisible () const
bool isNavigationBarVisible () const
QDate maximumDate () const
QDate minimumDate () const
int monthShown () const
QDate selectedDate () const
QCalendarWidget::SelectionMode selectionMode () const
void setDateEditAcceptDelay (int delay )
void setDateEditEnabled (bool enable )
void setDateTextFormat (QDate date , const QTextCharFormat & format )
void setFirstDayOfWeek (Qt::DayOfWeek dayOfWeek )
void setHeaderTextFormat (const QTextCharFormat & format )
void setHorizontalHeaderFormat (QCalendarWidget::HorizontalHeaderFormat format )
void setMaximumDate (QDate date )
void setMinimumDate (QDate date )
void setSelectionMode (QCalendarWidget::SelectionMode mode )
void setVerticalHeaderFormat (QCalendarWidget::VerticalHeaderFormat format )
void setWeekdayTextFormat (Qt::DayOfWeek dayOfWeek , const QTextCharFormat & format )
QCalendarWidget::VerticalHeaderFormat verticalHeaderFormat () const
QTextCharFormat weekdayTextFormat (Qt::DayOfWeek dayOfWeek ) const
int yearShown () const

重实现公共函数

virtual QSize minimumSizeHint () const override
virtual QSize sizeHint () const override

公共槽

void setCurrentPage (int year , int month )
void setDateRange (QDate min , QDate max )
void setGridVisible (bool show )
void setNavigationBarVisible (bool visible )
void setSelectedDate (QDate date )
void showNextMonth ()
void showNextYear ()
void showPreviousMonth ()
void showPreviousYear ()
void showSelectedDate ()
void showToday ()

信号

void activated (QDate date )
void clicked (QDate date )
void currentPageChanged (int year , int month )
void selectionChanged ()

保护函数

virtual void paintCell (QPainter * painter , const QRect & rect , QDate date ) const
void updateCell (QDate date )
void updateCells ()

重实现保护函数

virtual bool event (QEvent * event ) override
virtual bool eventFilter (QObject * watched , QEvent * event ) override
virtual void keyPressEvent (QKeyEvent * event ) override
virtual void mousePressEvent (QMouseEvent * event ) override
virtual void resizeEvent (QResizeEvent * event ) override

详细描述

Widget 的初始化是采用当前月和年,但 QCalendarWidget 有提供几个公共槽以改变显示的年和月。

默认情况下,选中今天的日期,且用户可以使用鼠标和键盘两者选择日期。可以检索目前选中的日期使用 selectedDate () 函数。它是可能的将用户选定约束到给定日期范围通过设置 minimumDate and maximumDate 特性。另外,可以一次性设置 2 特性使用 setDateRange () 方便槽。设置 selectionMode 特性到 NoSelection 以从根本上禁止用户选择。注意,还可以以编程方式选择日期使用 setSelectedDate () 槽。

可以检索目前显示的月和年使用 monthShown () 和 yearShown () 函数,分别。

新近创建的日历 Widget 使用缩写的天数名,且周六和周日两者以红色标记。日历栅格不可见。周数显示,且第 1 列天数是日历区域设置每周的第 1 天。

可以将天数的表示法更改为单字母缩写 (M 对于 Monday) 通过设置 horizontalHeaderFormat 特性到 QCalendarWidget::SingleLetterDayNames 。把同一特性设为 QCalendarWidget::LongDayNames 使 Header (头部) 显示完整天数名。可以移除周数通过设置 verticalHeaderFormat 特性到 QCalendarWidget::NoVerticalHeader 。可以打开日历栅格通过设置 gridVisible 特性为 true 使用 setGridVisible () 函数:

QCalendarWidget *calendar;
calendar->setGridVisible(true);
								

最后,可以更改第 1 列中的天数使用 setFirstDayOfWeek () 函数。

QCalendarWidget 类还提供了 3 信号, selectionChanged (), activated () 和 currentPageChanged () 使之可能响应用户交互。

可以很大程度上定制 Header (头)、工作日 (或单日) 的渲染通过设置 QTextCharFormat 为某些特殊工作日、特殊日期或 Header (头) 的渲染。

仅子集特性在 QTextCharFormat 可以用于日历 Widget。目前,前景、背景及字体特性可以用于确定 Widget 中单个单元格的渲染。

另请参阅 QDate , QDateEdit ,和 QTextCharFormat .

成员类型文档编制

enum QCalendarWidget:: HorizontalHeaderFormat

此枚举类型定义水平 Header (头) 可以显示的各种格式。

常量 描述
QCalendarWidget::SingleLetterDayNames 1 Header (头) 显示用于天数名的单字母缩写 (如 M 对于 Monday).
QCalendarWidget::ShortDayNames 2 Header (头) 显示天数名的简短缩写 (如 Mon 对于 Monday)。
QCalendarWidget::LongDayNames 3 Header (头) 显示完整天数名 (如:Monday)。
QCalendarWidget::NoHorizontalHeader 0 头被隐藏。

另请参阅 horizontalHeaderFormat () 和 VerticalHeaderFormat .

enum QCalendarWidget:: SelectionMode

此枚举描述提供给用户用于在日历中选择日期的选定类型。

常量 描述
QCalendarWidget::NoSelection 0 无法选择日期。
QCalendarWidget::SingleSelection 1 可以选择单个日期。

另请参阅 selectionMode .

enum QCalendarWidget:: VerticalHeaderFormat

此枚举类型定义垂直 Header (头) 可以显示的各种格式。

常量 描述
QCalendarWidget::ISOWeekNumbers 1 Header (头) 显示 ISO 周数如描述通过 QDate::weekNumber ().
QCalendarWidget::NoVerticalHeader 0 头被隐藏。

另请参阅 verticalHeaderFormat () 和 HorizontalHeaderFormat .

特性文档编制

dateEditAcceptDelay : int

此特性保持展示非活动日期编辑的时间,在接受其内容前

若日历 Widget 的 日期编辑被启用 , this property specifies the amount of time (in milliseconds) that the date edit remains open after the most recent user input. Once this time has elapsed, the date specified in the date edit is accepted and the popup is closed.

默认情况下,延迟被定义为 1500 毫秒 (1.5 秒)。

访问函数:

int dateEditAcceptDelay () const
void setDateEditAcceptDelay (int delay )

dateEditEnabled : bool

此特性保持是否启用日期编辑弹出

If this property is enabled, pressing a non-modifier key will cause a date edit to popup if the calendar widget has focus, allowing the user to specify a date in the form specified by the current locale.

By default, this property is enabled.

The date edit is simpler in appearance than QDateEdit , but allows the user to navigate between fields using the left and right cursor keys, increment and decrement individual fields using the up and down cursor keys, and enter values directly using the number keys.

访问函数:

bool isDateEditEnabled () const
void setDateEditEnabled (bool enable )

另请参阅 QCalendarWidget::dateEditAcceptDelay .

firstDayOfWeek : Qt::DayOfWeek

此特性保持第 1 列显示天数的标识值。

默认情况下,第 1 列显示的天数是日历区域设置每周的第 1 天。

访问函数:

Qt::DayOfWeek firstDayOfWeek () const
void setFirstDayOfWeek (Qt::DayOfWeek dayOfWeek )

gridVisible : bool

此特性保持是否显示表格栅格。

QCalendarWidget *calendar;
calendar->setGridVisible(true);
								

默认值为 false。

访问函数:

bool isGridVisible () const
void setGridVisible (bool show )

horizontalHeaderFormat : HorizontalHeaderFormat

此特性保持水平 Header (头) 的格式。

默认值为 QCalendarWidget::ShortDayNames .

访问函数:

QCalendarWidget::HorizontalHeaderFormat horizontalHeaderFormat () const
void setHorizontalHeaderFormat (QCalendarWidget::HorizontalHeaderFormat format )

maximumDate : QDate

此特性保持目前指定日期范围的最大日期。

The user will not be able to select a date which is after the currently set maximum date.

QCalendarWidget *calendar;
calendar->setGridVisible(true);
calendar->setMaximumDate(QDate(2006, 7, 3));
								

By default, the maximum date is the last day the QDate class can handle.

When setting a maximum date, the minimumDate and selectedDate properties are adjusted if the selection range becomes invalid. If the provided date is not a valid QDate object, the setMaximumDate() function does nothing.

访问函数:

QDate maximumDate () const
void setMaximumDate (QDate date )

另请参阅 setDateRange ().

minimumDate : QDate

This property holds the minimum date of the currently specified date range.

The user will not be able to select a date that is before the currently set minimum date.

QCalendarWidget *calendar;
calendar->setGridVisible(true);
calendar->setMinimumDate(QDate(2006, 6, 19));
								

By default, the minimum date is the earliest date that the QDate class can handle.

When setting a minimum date, the maximumDate and selectedDate properties are adjusted if the selection range becomes invalid. If the provided date is not a valid QDate object, the setMinimumDate() function does nothing.

访问函数:

QDate minimumDate () const
void setMinimumDate (QDate date )

另请参阅 setDateRange ().

navigationBarVisible : bool

This property holds whether the navigation bar is shown or not

When this property is true (the default), the next month, previous month, month selection, year selection controls are shown on top.

When the property is set to false, these controls are hidden.

访问函数:

bool isNavigationBarVisible () const
void setNavigationBarVisible (bool visible )

selectedDate : QDate

此特性保持目前选中的日期。

The selected date must be within the date range specified by the minimumDate and maximumDate properties. By default, the selected date is the current date.

访问函数:

QDate selectedDate () const
void setSelectedDate (QDate date )

另请参阅 setDateRange ().

selectionMode : SelectionMode

This property holds the type of selection the user can make in the calendar

当此特性被设为 SingleSelection , the user can select a date within the minimum and maximum allowed dates, using either the mouse or the keyboard.

当特性被设为 NoSelection , the user will be unable to select dates, but they can still be selected programmatically. Note that the date that is selected when the property is set to NoSelection will still be the selected date of the calendar.

默认值为 SingleSelection .

访问函数:

QCalendarWidget::SelectionMode selectionMode () const
void setSelectionMode (QCalendarWidget::SelectionMode mode )

verticalHeaderFormat : VerticalHeaderFormat

This property holds the format of the vertical header.

The default value is QCalendarWidget::ISOWeekNumber.

访问函数:

QCalendarWidget::VerticalHeaderFormat verticalHeaderFormat () const
void setVerticalHeaderFormat (QCalendarWidget::VerticalHeaderFormat format )

成员函数文档编制

QCalendarWidget:: QCalendarWidget ( QWidget * parent = nullptr)

构造日历 Widget 采用给定 parent .

The widget is initialized with the current month and year, and the currently selected date is today.

另请参阅 setCurrentPage ().

[signal] void QCalendarWidget:: activated ( QDate date )

This signal is emitted whenever the user presses the Return or Enter key or double-clicks a date in the calendar widget.

[signal] void QCalendarWidget:: clicked ( QDate date )

This signal is emitted when a mouse button is clicked. The date the mouse was clicked on is specified by date . The signal is only emitted when clicked on a valid date, e.g., dates are not outside the minimumDate () 和 maximumDate (). If the selection mode is NoSelection , this signal will not be emitted.

[signal] void QCalendarWidget:: currentPageChanged ( int year , int month )

This signal is emitted when the currently shown month is changed. The new year and month are passed as parameters.

另请参阅 setCurrentPage ().

[signal] void QCalendarWidget:: selectionChanged ()

此信号被发射,当目前选中日期改变时。

The currently selected date can be changed by the user using the mouse or keyboard, or by the programmer using setSelectedDate ().

另请参阅 selectedDate ().

[slot] void QCalendarWidget:: setCurrentPage ( int year , int month )

显示给定 month 为给定 year without changing the selected date. Use the setSelectedDate () function to alter the selected date.

可以检索目前显示的月和年使用 monthShown () 和 yearShown () 函数分别。

另请参阅 yearShown (), monthShown (), showPreviousMonth (), showNextMonth (), showPreviousYear (),和 showNextYear ().

[slot] void QCalendarWidget:: setDateRange ( QDate min , QDate max )

定义日期范围通过设置 minimumDate and maximumDate 特性。

The date range restricts the user selection, i.e. the user can only select dates within the specified date range. Note that

QCalendarWidget *calendar;
calendar->setDateRange(min, max);
					

is analogous to

QCalendarWidget *calendar;
calendar->setMinimumDate(min);
calendar->setMaximumDate(max);
					

min or max 参数是无效 QDate 对象,此函数什么都不做。

另请参阅 setMinimumDate () 和 setMaximumDate ().

[slot] void QCalendarWidget:: showNextMonth ()

Shows the next month relative to the currently displayed month. Note that the selected date is not changed.

另请参阅 showPreviousMonth (), setCurrentPage (),和 setSelectedDate ().

[slot] void QCalendarWidget:: showNextYear ()

Shows the currently displayed month in the next year relative to the currently displayed year. Note that the selected date is not changed.

另请参阅 showPreviousYear (), setCurrentPage (),和 setSelectedDate ().

[slot] void QCalendarWidget:: showPreviousMonth ()

Shows the previous month relative to the currently displayed month. Note that the selected date is not changed.

另请参阅 showNextMonth (), setCurrentPage (),和 setSelectedDate ().

[slot] void QCalendarWidget:: showPreviousYear ()

Shows the currently displayed month in the previous year relative to the currently displayed year. Note that the selected date is not changed.

另请参阅 showNextYear (), setCurrentPage (),和 setSelectedDate ().

[slot] void QCalendarWidget:: showSelectedDate ()

Shows the month of the selected date.

另请参阅 selectedDate () 和 setCurrentPage ().

[slot] void QCalendarWidget:: showToday ()

Shows the month of the today's date.

另请参阅 selectedDate () 和 setCurrentPage ().

[虚拟] QCalendarWidget:: ~QCalendarWidget ()

Destroys the calendar widget.

QMap < QDate , QTextCharFormat > QCalendarWidget:: dateTextFormat () const

返回 QMap from QDate to QTextCharFormat showing all dates that use a special format that alters their rendering.

另请参阅 setDateTextFormat ().

QTextCharFormat QCalendarWidget:: dateTextFormat ( QDate date ) const

返回 QTextCharFormat for date . The char format can be be empty if the date is not renderd specially.

[override virtual protected] bool QCalendarWidget:: event ( QEvent * event )

重实现: QWidget::event (QEvent *event).

[override virtual protected] bool QCalendarWidget:: eventFilter ( QObject * watched , QEvent * event )

重实现: QObject::eventFilter (QObject *watched, QEvent *event).

QTextCharFormat QCalendarWidget:: headerTextFormat () const

Returns the text char format for rendering the header.

另请参阅 setHeaderTextFormat ().

[override virtual protected] void QCalendarWidget:: keyPressEvent ( QKeyEvent * event )

重实现: QWidget::keyPressEvent (QKeyEvent *event).

[override virtual] QSize QCalendarWidget:: minimumSizeHint () const

重实现访问函数为特性: QWidget::minimumSizeHint .

int QCalendarWidget:: monthShown () const

Returns the currently displayed month. Months are numbered from 1 to 12.

另请参阅 yearShown () 和 setCurrentPage ().

[override virtual protected] void QCalendarWidget:: mousePressEvent ( QMouseEvent * event )

重实现: QWidget::mousePressEvent (QMouseEvent *event).

[virtual protected] void QCalendarWidget:: paintCell ( QPainter * painter , const QRect & rect , QDate date ) const

描绘单元格指定通过给定 date ,使用给定 painter and rect .

[override virtual protected] void QCalendarWidget:: resizeEvent ( QResizeEvent * event )

重实现: QWidget::resizeEvent (QResizeEvent *event).

void QCalendarWidget:: setDateTextFormat ( QDate date , const QTextCharFormat & format )

Sets the format used to render the given date to that specified by format .

date 为 null,清零所有日期格式。

另请参阅 dateTextFormat ().

void QCalendarWidget:: setHeaderTextFormat (const QTextCharFormat & format )

Sets the text char format for rendering the header to format . If you also set a weekday text format, this format's foreground and background color will take precedence over the header's format. The other formatting information will still be decided by the header's format.

另请参阅 headerTextFormat ().

void QCalendarWidget:: setWeekdayTextFormat ( Qt::DayOfWeek dayOfWeek , const QTextCharFormat & format )

Sets the text char format for rendering of day in the week dayOfWeek to format . The format will take precedence over the header format in case of foreground and background color. Other text formatting information is taken from the headers format.

另请参阅 weekdayTextFormat () 和 setHeaderTextFormat ().

[override virtual] QSize QCalendarWidget:: sizeHint () const

重实现访问函数为特性: QWidget::sizeHint .

[protected] void QCalendarWidget:: updateCell ( QDate date )

Updates the cell specified by the given date unless updates are disabled or the cell is hidden.

另请参阅 updateCells (), yearShown (),和 monthShown ().

[protected] void QCalendarWidget:: updateCells ()

更新所有可见单元格,除非更新被禁用。

另请参阅 updateCell ().

QTextCharFormat QCalendarWidget:: weekdayTextFormat ( Qt::DayOfWeek dayOfWeek ) const

返回的文本字符格式用于渲染天按周 dayOfWeek .

另请参阅 setWeekdayTextFormat () 和 headerTextFormat ().

int QCalendarWidget:: yearShown () const

返回目前显示月的年。月的编号是从 1 到 12。

另请参阅 monthShown () 和 setCurrentPage ().