QCalendarWidget Class

QCalendarWidget 提供一个日历widget, 允许用户选择日期. 更多...

头文件: #include <QCalendarWidget>
qmake: QT += widgets
开始支持版本: Qt 4.2
基类: QWidget

公有类型

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

属性

公有函数

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

重新实现的公有函数

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

公有槽函数

void setCurrentPage(int year, int month)
void setDateRange(const QDate &min, const QDate &max)
void setGridVisible(bool show)
void setNavigationBarVisible(bool visible)
void setSelectedDate(const QDate &date)
void showNextMonth()
void showNextYear()
void showPreviousMonth()
void showPreviousYear()
void showSelectedDate()
void showToday()
  • 19 个公有槽函数继承自 QWidget
  • 1 个公有槽函数继承自 QObject

信号

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

受保护的函数

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

重新实现的受保护函数

virtual bool event(QEvent *event)
virtual bool eventFilter(QObject *watched, QEvent *event)
virtual void keyPressEvent(QKeyEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
virtual void resizeEvent(QResizeEvent *event)
  • 35 个受保护的函数继承自 QWidget
  • 9 个受保护的函数继承自 QObject
  • 1 个受保护的函数继承自 QPaintDevice

其他继承的成员

  • 1 个公有变量继承自 QObject
  • 5 个静态公有成员继承自 QWidget
  • 10 个静态公有成员继承自 QObject
  • 1 个受保护的槽函数继承自 QWidget
  • 2 个受保护的变量继承自 QObject
  • 1 protected type inherited from QPaintDevice

详细描述

QCalendarWidget 提供一个日历widget, 允许用户选择日期.

The widget is initialized with the current month and year, but QCalendarWidget provides several public slots to change the year and month that is shown.

By default, today's date is selected, and the user can select a date using both mouse and keyboard. The currently selected date can be retrieved using the selectedDate() function. It is possible to constrain the user selection to a given date range by setting the minimumDate and maximumDate properties. Alternatively, both properties can be set in one go using the setDateRange() convenience slot. Set the selectionMode property to NoSelection to prohibit the user from selecting at all. Note that a date also can be selected programmatically using the setSelectedDate() slot.

The currently displayed month and year can be retrieved using the monthShown() and yearShown() functions, respectively.

A newly created calendar widget uses abbreviated day names, and both Saturdays and Sundays are marked in red. The calendar grid is not visible. The week numbers are displayed, and the first column day is the first day of the week for the calendar's locale.

The notation of the days can be altered to a single letter abbreviations ("M" for "Monday") by setting the horizontalHeaderFormat property to QCalendarWidget::SingleLetterDayNames. Setting the same property to QCalendarWidget::LongDayNames makes the header display the complete day names. The week numbers can be removed by setting the verticalHeaderFormat property to QCalendarWidget::NoVerticalHeader. The calendar grid can be turned on by setting the gridVisible property to true using the setGridVisible() function:


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

Finally, the day in the first column can be altered using the setFirstDayOfWeek() function.

The QCalendarWidget class also provides three signals, selectionChanged(), activated() and currentPageChanged() making it possible to respond to user interaction.

The rendering of the headers, weekdays or single days can be largely customized by setting QTextCharFormat's for some special weekday, a special date or for the rendering of the headers.

Only a subset of the properties in QTextCharFormat are used by the calendar widget. Currently, the foreground, background and font properties are used to determine the rendering of individual cells in the widget.

参见 QDate, QDateEdit, and QTextCharFormat.

成员类型

enum QCalendarWidget::HorizontalHeaderFormat

This enum type defines the various formats the horizontal header can display.

ConstantValueDescription
QCalendarWidget::SingleLetterDayNames1The header displays a single letter abbreviation for day names (e.g. M for Monday).
QCalendarWidget::ShortDayNames2The header displays a short abbreviation for day names (e.g. Mon for Monday).
QCalendarWidget::LongDayNames3The header displays complete day names (e.g. Monday).
QCalendarWidget::NoHorizontalHeader0The header is hidden.

参见 horizontalHeaderFormat() and VerticalHeaderFormat.

enum QCalendarWidget::SelectionMode

This enum describes the types of selection offered to the user for selecting dates in the calendar.

ConstantValueDescription
QCalendarWidget::NoSelection0Dates cannot be selected.
QCalendarWidget::SingleSelection1Single dates can be selected.

参见 selectionMode.

enum QCalendarWidget::VerticalHeaderFormat

This enum type defines the various formats the vertical header can display.

ConstantValueDescription
QCalendarWidget::ISOWeekNumbers1The header displays ISO week numbers as described by QDate::weekNumber().
QCalendarWidget::NoVerticalHeader0The header is hidden.

参见 verticalHeaderFormat() and HorizontalHeaderFormat.

属性

dateEditAcceptDelay : int

This property holds the time an inactive date edit is shown before its contents are accepted

If the calendar widget's date edit is enabled, this property specifies the amount of time (in millseconds) 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.

By default, the delay is defined to be 1500 milliseconds (1.5 seconds).

This property was introduced in Qt 4.3.

访问函数:

int dateEditAcceptDelay() const
void setDateEditAcceptDelay(int delay)

dateEditEnabled : bool

This property holds whether the date edit popup is enabled

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.

This property was introduced in Qt 4.3.

访问函数:

bool isDateEditEnabled() const
void setDateEditEnabled(bool enable)

参见 QCalendarWidget::dateEditAcceptDelay.

firstDayOfWeek : Qt::DayOfWeek

This property holds a value identifying the day displayed in the first column.

By default, the day displayed in the first column is the first day of the week for the calendar's locale.

访问函数:

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

gridVisible : bool

This property holds whether the table grid is displayed.


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

The default value is false.

访问函数:

bool isGridVisible() const
void setGridVisible(bool show)

horizontalHeaderFormat : HorizontalHeaderFormat

This property holds the format of the horizontal header.

The default value is QCalendarWidget::ShortDayNames.

访问函数:

HorizontalHeaderFormat horizontalHeaderFormat() const
void setHorizontalHeaderFormat(HorizontalHeaderFormat format)

maximumDate : QDate

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

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(const 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(const QDate &date)

参见 setDateRange().

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.

This property was introduced in Qt 4.3.

访问函数:

bool isNavigationBarVisible() const
void setNavigationBarVisible(bool visible)

selectedDate : QDate

This property holds the currently selected date.

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(const QDate &date)

参见 setDateRange().

selectionMode : SelectionMode

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

When this property is set to SingleSelection, the user can select a date within the minimum and maximum allowed dates, using either the mouse or the keyboard.

When the property is set to 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.

The default value is SingleSelection.

访问函数:

SelectionMode selectionMode() const
void setSelectionMode(SelectionMode mode)

verticalHeaderFormat : VerticalHeaderFormat

This property holds the format of the vertical header.

The default value is QCalendarWidget::ISOWeekNumber.

访问函数:

VerticalHeaderFormat verticalHeaderFormat() const
void setVerticalHeaderFormat(VerticalHeaderFormat format)

成员函数

QCalendarWidget::QCalendarWidget(QWidget *parent = Q_NULLPTR)

Constructs a calendar widget with the given parent.

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

参见 setCurrentPage().

QCalendarWidget::~QCalendarWidget()

Destroys the calendar widget.

[signal] void QCalendarWidget::activated(const 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(const 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() and 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().

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

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

参见 setDateTextFormat().

QTextCharFormat QCalendarWidget::dateTextFormat(const QDate &date) const

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

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

重新实现 QObject::event().

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

重新实现 QObject::eventFilter().

QTextCharFormat QCalendarWidget::headerTextFormat() const

Returns the text char format for rendering the header.

参见 setHeaderTextFormat().

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

重新实现 QWidget::keyPressEvent().

[virtual] QSize QCalendarWidget::minimumSizeHint() const

重新实现 QWidget::minimumSizeHint().

int QCalendarWidget::monthShown() const

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

参见 yearShown() and setCurrentPage().

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

重新实现 QWidget::mousePressEvent().

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

Paints the cell specified by the given date, using the given painter and rect.

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

重新实现 QWidget::resizeEvent().

[signal] void QCalendarWidget::selectionChanged()

This signal is emitted when the currently selected date is changed.

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)

Displays the given month of the given year without changing the selected date. Use the setSelectedDate() function to alter the selected date.

The currently displayed month and year can be retrieved using the monthShown() and yearShown() functions respectively.

参见 yearShown(), monthShown(), showPreviousMonth(), showNextMonth(), showPreviousYear(), and showNextYear().

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

Defines a date range by setting the minimumDate and maximumDate properties.

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);

If either the min or max parameters are not valid QDate objects, this function does nothing.

参见 setMinimumDate() and setMaximumDate().

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

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

If date is null, all date formats are cleared.

参见 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() and setHeaderTextFormat().

[slot] void QCalendarWidget::showNextMonth()

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

参见 showPreviousMonth(), setCurrentPage(), and 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(), and 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(), and 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(), and setSelectedDate().

[slot] void QCalendarWidget::showSelectedDate()

Shows the month of the selected date.

参见 selectedDate() and setCurrentPage().

[slot] void QCalendarWidget::showToday()

Shows the month of the today's date.

参见 selectedDate() and setCurrentPage().

[virtual] QSize QCalendarWidget::sizeHint() const

重新实现 QWidget::sizeHint().

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

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

This function was introduced in Qt 4.4.

参见 updateCells(), yearShown(), and monthShown().

[protected] void QCalendarWidget::updateCells()

Updates all visible cells unless updates are disabled.

This function was introduced in Qt 4.4.

参见 updateCell().

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

Returns the text char format for rendering of day in the week dayOfWeek.

参见 setWeekdayTextFormat() and headerTextFormat().

int QCalendarWidget::yearShown() const

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

参见 monthShown() and setCurrentPage().