(1)
(2)
(3)
(4)集中测试本日历里的信号发射函数 :
++
++ 测试结果 :
(5) 本源代码来自于 qcalendarwidget . h :
#ifndef QCALENDARWIDGET_H
#define QCALENDARWIDGET_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtWidgets/qwidget.h>
#include <QtCore/qdatetime.h>
QT_REQUIRE_CONFIG(calendarwidget);
QT_BEGIN_NAMESPACE //说明本类定义于 Qt 的全局命名空间
class QDate;
class QTextCharFormat;
class QCalendarWidgetPrivate;
/*
The QCalendarWidget class provides a monthly based calendar widget allowing the
user to select a date.
该小部件初始化为当前月份和年份,但 QCalendarWidget提供了几个公共槽位,用于更改显示的年份和月份。
默认情况下,选择今天的日期,用户可以使用鼠标和键盘选择日期。
可以使用 selectedDate()函数检索当前选择的日期。
通过设置 minimumDate和 maximumDate属性,可以限制用户选择给定的日期范围。
或者,可以使用 setDateRange()便利插槽一次性设置这两个属性。
将 selectionMode属性设置为 NoSelection以禁止用户进行选择。
请注意,也可以使用 setSelectedDate()插槽以编程方式选择日期。
当前显示的月份和年份可以使用 monthShown()和 yearShown()函数分别检索。
-个新创建的日历小部件使用简略的日期名称,星期六和星期日都标记为红色。日历网格不可见。
The week numbers are displayed,
第一列的日期是日历所在区域的星期的第一天。
可以通过将 horizontalHeaderFormat属性设置为 QCalendarWidget::SingleLetterDayNames来将
日期的符号更改为单个字母缩写(“M”表示“星期一”)。
通过将相同的属性设置为 QCalendarWidget::LongDayNames,可以使标题显示完整的日期名称。
可以通过将 verticalHeaderFormat属性设置为 QCalendarWidget:NoVerticalHeader来删除
星期几的数字。
可以通过使用 setGridVisible()函数将 gridVisible属性设置为 true来打开日历网格。
最后,可以使用 setFirstDayOfWeek()函数更改第一列中的日期。
QCalendarWidget类还提供了三个信号:selectionChanged()、activated()和currentPageChanged(),
使得可以响应用户交互。
通过设置 QTextCharFormat的某些特殊工作日、特殊日期或用于渲染标题,
可以很大程度上自定义标题、工作日或单个日期的显示。
日历控件只使用QTextCharFormat属性中的子集。目前,前景、背景和字体属性用于确定控件中单个单元格的渲染。
*/
class Q_WIDGETS_EXPORT QCalendarWidget : public QWidget
{
Q_OBJECT
//enum Qt::DayOfWeek{ Monday=1, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
Q_ENUMS(Qt::DayOfWeek)
Q_PROPERTY(Qt::DayOfWeek firstDayOfWeek //确定每周里的第一天是星期几,并将之显示在第一列
READ firstDayOfWeek WRITE setFirstDayOfWeek)
//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.
//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.
Q_PROPERTY(QDate selectedDate READ selectedDate WRITE setSelectedDate)
//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. //默认的最小日期 1752/ 9/14
Q_PROPERTY(QDate minimumDate READ minimumDate WRITE setMinimumDate)
Q_PROPERTY(QDate maximumDate READ maximumDate WRITE setMaximumDate)
//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. //默认的最大日期 9999/12/31
//This property holds whether the table grid is displayed. default value is false.
Q_PROPERTY(bool gridVisible READ isGridVisible WRITE setGridVisible)
//This property holds the format of the horizontal header.
//The default value is QCalendarWidget::ShortDayNames. //举例 一 周一 星期一
Q_PROPERTY(HorizontalHeaderFormat horizontalHeaderFormat
READ horizontalHeaderFormat WRITE setHorizontalHeaderFormat)
Q_PROPERTY(VerticalHeaderFormat verticalHeaderFormat //列里显示每个周的年序号
READ verticalHeaderFormat WRITE setVerticalHeaderFormat)
//his property holds the format of the vertical header.
//The default value is QCalendarWidget::ISOWeekNumber.
//This property holds whether the navigation bar is shown or not。//导航栏是否可见
//When this property is true (the default), previous month <-, the next month ->,
// month selection 当前月份, year selection当前年份 controls are shown on top.
//When the property is set to false, these controls are hidden.
Q_PROPERTY(bool navigationBarVisible
READ isNavigationBarVisible WRITE setNavigationBarVisible)
//此属性存储用户可以在日历中进行的选定的类型。
//当此属性设置为“单选”时,用户可以使用鼠标或键盘在允许的最小和最大日期范围内选择一个日期。
//当该属性设置为 NoSelection 时,用户将无法选择日期,但他们仍然可以通过编程选择日期。
//请注意,当该属性设置为 NoSelection 时选择的日期仍然是日历的新的选定日期。
Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
//这个属性表示是否启用了日期编辑弹出窗口。如果启用了此属性,
//按下非修改键将在日历小部件获得焦点时弹出一个日期编辑框,允许用户以当前区域设置指定的格式指定日期。
//默认情况下,此属性已启用。日期编辑在外观上比QDateEdit更简单,
//但允许用户使用左右光标键在字段之间导航,使用上下光标键增加和减少单个字段,并使用数字键直接输入值。
Q_PROPERTY(bool dateEditEnabled //对应一个小的弹出框显示焦点处的日期:2025/9/2
READ isDateEditEnabled WRITE setDateEditEnabled)
Q_PROPERTY(int dateEditAcceptDelay //指弹出的小日期框静止状态下的存活时长
READ dateEditAcceptDelay WRITE setDateEditAcceptDelay)
//此属性存储非活动日期编辑在其内容被接受之前显示的时间。This property holds the time an
// inactive date edit is shown before its contents are accepted。
//如果日历小部件的日期编辑被启用,此属性指定在用户最近输入后日期编辑保持打开的时间量(以毫秒为单位)。
//一旦这段时间过去,日期编辑中指定的日期将被接受,弹出窗口将被关闭。
//默认情况下,延迟被定义为1500毫秒(1.5秒)
private: //本日历类并未在 QWidget的基础上新建数据成员
Q_DECLARE_PRIVATE(QCalendarWidget)
Q_DISABLE_COPY(QCalendarWidget)
Q_PRIVATE_SLOT(d_func(), void _q_slotShowDate(QDate date))
Q_PRIVATE_SLOT(d_func(), void _q_slotChangeDate(QDate date))
Q_PRIVATE_SLOT(d_func(), void _q_slotChangeDate(QDate date, bool changeMonth))
Q_PRIVATE_SLOT(d_func(), void _q_editingFinished())
Q_PRIVATE_SLOT(d_func(), void _q_prevMonthClicked())
Q_PRIVATE_SLOT(d_func(), void _q_nextMonthClicked())
Q_PRIVATE_SLOT(d_func(), void _q_yearEditingFinished())
Q_PRIVATE_SLOT(d_func(), void _q_yearClicked())
Q_PRIVATE_SLOT(d_func(), void _q_monthChanged(QAction *act))
public:
enum HorizontalHeaderFormat {
NoHorizontalHeader , //行头隐藏
SingleLetterDayNames, // 一
ShortDayNames, // 周一
LongDayNames //星期一
};
Q_ENUM(HorizontalHeaderFormat)
enum VerticalHeaderFormat {
NoVerticalHeader, //列头隐藏
ISOWeekNumbers //ISO 给的本周在本年里的周序号 1--52
};
Q_ENUM(VerticalHeaderFormat)
enum SelectionMode {
NoSelection, //这个值将禁止用户从日历里选择日期。但可以由程序编程修改
SingleSelection //默认值
};
Q_ENUM(SelectionMode)
//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.
explicit QCalendarWidget(QWidget * parent = nullptr);
~QCalendarWidget();
//Q_PROPERTY(Qt::DayOfWeek firstDayOfWeek //确定每周里的第一天是星期几,并将之显示在第一列
// READ firstDayOfWeek WRITE setFirstDayOfWeek)
Qt::DayOfWeek firstDayOfWeek() const;
void setFirstDayOfWeek(Qt::DayOfWeek dayOfWeek);
//Q_PROPERTY(QDate selectedDate READ selectedDate WRITE setSelectedDate)
QDate selectedDate() const;
public Q_SLOTS:
void setSelectedDate(QDate date);
public :
//Q_PROPERTY(QDate minimumDate READ minimumDate WRITE setMinimumDate)
QDate minimumDate() const;
void setMinimumDate(QDate date);
//Q_PROPERTY(QDate maximumDate READ maximumDate WRITE setMaximumDate)
QDate maximumDate() const;
void setMaximumDate(QDate date);
//Q_PROPERTY(bool gridVisible READ isGridVisible WRITE setGridVisible)
bool isGridVisible() const;
public Q_SLOTS:
void setGridVisible(bool show);
public :
//Q_PROPERTY(HorizontalHeaderFormat horizontalHeaderFormat //水平行头
// READ horizontalHeaderFormat WRITE setHorizontalHeaderFormat)
HorizontalHeaderFormat horizontalHeaderFormat() const;
void setHorizontalHeaderFormat(HorizontalHeaderFormat format);
//Q_PROPERTY(VerticalHeaderFormat verticalHeaderFormat //列里显示每个周的年序号
// READ verticalHeaderFormat WRITE setVerticalHeaderFormat)
VerticalHeaderFormat verticalHeaderFormat() const;
void setVerticalHeaderFormat(VerticalHeaderFormat format);
//Q_PROPERTY(bool navigationBarVisible //最上面的导航栏是否可见
// READ isNavigationBarVisible WRITE setNavigationBarVisible)
bool isNavigationBarVisible() const;
public Q_SLOTS:
void setNavigationBarVisible(bool visible);
public :
//Q_PROPERTY(SelectionMode selectionMode READ selectionMode WRITE setSelectionMode)
SelectionMode selectionMode() const;
void setSelectionMode(SelectionMode mode);
//Q_PROPERTY(bool dateEditEnabled //对应一个小的弹出框显示焦点处的日期:2025/9/2
// READ isDateEditEnabled WRITE setDateEditEnabled)
bool isDateEditEnabled() const;
void setDateEditEnabled(bool enable);
//Q_PROPERTY(int dateEditAcceptDelay //指弹出的小日期框静止状态下的存活时长
// READ dateEditAcceptDelay WRITE setDateEditAcceptDelay)
int dateEditAcceptDelay() const;
void setDateEditAcceptDelay(int delay);
QCalendar calendar() const; //无注释
void setCalendar(QCalendar calendar);
QTextCharFormat headerTextFormat () const; //不用管这些格式
void setHeaderTextFormat (const QTextCharFormat & format);
QTextCharFormat weekdayTextFormat(Qt::DayOfWeek dayOfWeek) const;
void setWeekdayTextFormat(Qt::DayOfWeek dayOfWeek,
const QTextCharFormat & format);
QMap<QDate, QTextCharFormat> dateTextFormat() const;
QTextCharFormat dateTextFormat(QDate date) const;
void setDateTextFormat(QDate date,
const QTextCharFormat & format);
//Returns the currently displayed month. Months are numbered from 1 to 12.
int monthShown() const;
int yearShown() const;
//Returns the year of the currently displayed month.
//Months are numbered from 1 to 12.
virtual QSize sizeHint() const override;
virtual QSize minimumSizeHint() const override;
public Q_SLOTS:
//void setGridVisible (bool show);
//void setNavigationBarVisible(bool visible);
//void setSelectedDate(QDate date);
void setCurrentPage(int year, int month) ; //设置当前显示的月份
void setDateRange (QDate min, QDate max);
void showSelectedDate(); //Shows the month of the selected date.
void showToday(); //Shows the month of the today's date.
//Shows the previous month relative to the currently displayed month.
//Note that the selected date is not changed.
void showPreviousMonth(); //这些 show()函数没有返回值,测试里也没发现什么变化,
void showNextMonth(); //暂时先放这里
//Shows the next month relative to the currently displayed month.
//Note that the selected date is not changed.
//Shows the currently displayed month in the previous year relative to the
//currently displayed year.
void showPreviousYear();
void showNextYear();
//Shows the currently displayed month in the next year relative to the
//currently displayed year. Note that the selected date is not changed.
Q_SIGNALS:
//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.
void clicked (QDate date); //只有敲击有效的日期值,才会触发 clicked() 信号
void activated(QDate date); //双击可以触发该信号,同时也会多给触发一次 clicked信号
//This signal is emitted whenever the user presses the Return or Enter key or
//double-clicks a date in the calendar widget.
//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().
void selectionChanged(); //只有选中的日期改变了才会触发本信号。重复单击同一日期不触发本新信号
void 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.
protected:
//Paints the cell specified by the given date, using the given painter and rect.
virtual void paintCell(QPainter * painter, const QRect & rect, QDate date) const;
//Updates the cell specified by the given date unless updates are disabled
//or the cell is hidden.
void updateCell(QDate date);
void updateCells();
//Updates all visible cells unless updates are disabled.
bool event(QEvent * event) override;
void resizeEvent(QResizeEvent * event) override;
void mousePressEvent(QMouseEvent * event) override;
void keyPressEvent(QKeyEvent * event) override;
bool eventFilter(QObject * watched, QEvent * event) override;
}; //完结 class QCalendarWidget : public QWidget
QT_END_NAMESPACE
#endif // QCALENDARWIDGET_H
(6)
谢谢