(1)这些控件具有螺旋输入框的样式,这些类也确实继承于 QAbstractSpinBox 这个类:
(2)开始父类 QDateTimeEdit 这个输入控件的成员函数于属性测试 :
++
(3)
(4)
++
++
++
++
(5)子类 QTimeEdit、QDateEdit 没有对父类 QDateTimeEdit 做大的扩充,就不举例了:
(6)本源代码来自于头文件 qdatetimeedit . h :
#ifndef QDATETIMEEDIT_H
#define QDATETIMEEDIT_H
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qdatetime.h>
#include <QtCore/qcalendar.h>
#include <QtCore/qvariant.h>
#include <QtWidgets/qabstractspinbox.h>
QT_REQUIRE_CONFIG(datetimeedit);
QT_BEGIN_NAMESPACE //说明本类定义在 QT的全局命名空间
class QDateTimeEditPrivate;
class QStyleOptionSpinBox;
class QCalendarWidget;
/*
The QDateTimeEdit class provides a widget for editing dates and times.
QDateTimeEdit允许用户通过使用键盘或箭头键来增加和减少日期和时间值来编辑日期。
箭头键可用于在QDateTimeEdit框内从一个部分移动到另一个部分。
日期和时间按照设置的格式显示;请参阅setDisplayFormat()。
QDateTimeEdit的有效值范围由minimumDateTime、maximumDateTime属性及其各自的日期和时间组件控制。
默认情况下,从公元100年1月1日到公元9999年12月31日的任何日期和时间都是有效的。
Using a Pop-up Calendar Widget:
QDateTimeEdit 可以配置为允许使用 QCalendarWidget来选择日期。这通过设置calendarPopup属性来实现。
此外,您可以通过调用setCalendarWidget()函数来提供一个自定义的日历控件作为日历弹出窗口。
现有的日历控件可以通过 calendarWidget()函数检索。
Keyboard Tracking:
当启用键盘跟踪(默认为启用)时,编辑字段的每个键击都会触发值更改的信号。
当允许的范围比它跨越的某个时间间隔更窄时,键盘跟踪会阻止用户编辑日期或时间以访问时间间隔的后半部分。
例如,对于从 2020年4月29日到 2020年5月2日的范围以及初始日期为 2020年4月30日,
用户既不能更改月份(5月30日不在范围内),也不能更改日期(4月2日不在范围内)。
当禁用键盘跟踪时,只有在编辑修改内容后焦点离开文本字段时才会发出更改信号。
这允许用户通过无效的日期时间到达有效的日期时间进行编辑。
This allows the user to edit via an invalid date-time to reach a valid one。
*/
class Q_WIDGETS_EXPORT QDateTimeEdit : public QAbstractSpinBox
{
Q_OBJECT //又插入了此宏,以使用 QT的元对象系统
//此属性持有在 QDateTimeEdit 中设置的 QDateTime。
//设置此属性时,新 QDateTime 会被转换为 QDateTimeEdit的timespec, timespec保持不变。
//默认情况下,此属性设置为2000年1月1日。它只能设置为有效的QDateTime值。
//如果任何操作导致此属性具有无效的日期时间作为值,则将其重置为 minimumDateTime属性的值。
//如果 ODateTimeEdit 没有日期字段,则设置此属性会将该小部件的日期范围设置为
//以该属性的新值日期开始和结束。
Q_PROPERTY(QDateTime dateTime READ dateTime WRITE setDateTime
NOTIFY dateTimeChanged USER true)
Q_PROPERTY(QDate date READ date WRITE setDate
NOTIFY dateChanged) //Returns the date of the date time edit.
Q_PROPERTY(QTime time READ time WRITE setTime
NOTIFY timeChanged) //Returns the time of the date time edit.
//此属性保存日期时间编辑的最大日期时间。
//更改此属性会隐式地将最大日期和最大时间属性更新为此属性的日期和时间部分。
//在设置此属性时,如果需要,会调整最小日期时间以确保范围仍然有效。
// 否则,更改此属性会保留最小日期时间属性。
//此属性只能设置为有效的 QDateTime值。
//setMaximumDateTime()接受的最近日期和时间是公元 9999 年的最后一天。这是此属性的默认值。
//可以通过clearMaximumDateTime()恢复此默认值。
Q_PROPERTY(QDateTime maximumDateTime READ maximumDateTime WRITE setMaximumDateTime
RESET clearMaximumDateTime) //默认最大值 9999/12/31 23:59:59
Q_PROPERTY(QDateTime minimumDateTime READ minimumDateTime WRITE setMinimumDateTime
RESET clearMinimumDateTime) //默认最小值 1752/ 9/14 0:00:00
//此属性包含日期时间编辑的最低日期时间。
//更改此属性将隐式更新最小日期和最小时间属性,分别更新为此属性的日期和时间部分。
//在设置此属性时,如果需要,会调整最大日期时间以确保范围仍然有效。
//否则,更改此属性会保留最大日期时间属性。
//此属性只能设置为有效的 QDateTime值。
//setMinimumDateTime()接受的最早日期和时间是公元 100 年的开始。
//属性的默认值是公元1752年9月14日的开始。这个默认值可以通过clearMinimumDateTime()恢复。
//此属性包含日期时间编辑的最大日期。
//更改此属性会更新 maximumDateTime属性的日期,同时保留 maximumTime属性。
//在设置此属性时,如果需要,会调整最小日期以确保范围仍然有效。
//当发生这种情况时如果最小Time属性大于最大Time属性,则也会调整最小Time属性。
//否则,更改此属性会保留 minimumDateTime属性。
//此属性只能设置为描述当前最大时间属性生成有效 QDateTime 对象的日期的有效 QDate 对象。
//setaximumDate( 接受的最后日期是公元9999年底的日期。这是此属性的默认值。
//可以通过clearMaximumDateTime()恢复此默认值。
Q_PROPERTY(QDate maximumDate READ maximumDate WRITE setMaximumDate
RESET clearMaximumDate)
Q_PROPERTY(QDate minimumDate READ minimumDate WRITE setMinimumDate
RESET clearMinimumDate)
//此属性包含日期时间编辑的最低日期。
//更改此属性会更新 minimumDateTime属性的日期,同时保留 minimumTime属性。
//设置此属性时,如果需要,会调整最大日期,以确保范围仍然有效。
//当发生这种情况时,如果最大Time属性小于最小Time属性,则也会调整最大Time属性。
// 否则,更改此属性会保留最大DateTime属性。
//此属性只能设置为描述当前最小时间属性生成有效 QDateTime 对象的日期的有效QDate 对象。
//setMinimumDate()接受的最早日期是公元100年的开始。
//此属性的默认值是公元1752年9月14日。可以通过 clearMinimumDateTime()恢复此默认值。
//此属性保存日期时间编辑的最大时间。
//更改此属性会更新 MaximumDateTime属性的时间,同时保留 minimumDate和 maximumDate属性.
//如果这些日期属性重合,在设置此属性时,如果需要,会调整最小Time属性,以确保范围仍然有效。
// 否则,更改此属性会保留最小DateTime属性。
//此属性可以设置为任何有效的 QTime 值。默认情况下,此属性包含时间为 23:59:59 和 999 毫秒。
//可以通过 clearMaximumTime() 恢复默认值。
Q_PROPERTY(QTime maximumTime READ maximumTime WRITE setMaximumTime
RESET clearMaximumTime) //默认的最大时间值 23:59:59.999
Q_PROPERTY(QTime minimumTime READ minimumTime WRITE setMinimumTime
RESET clearMinimumTime) //默认的最小时间值 0:00:00.000
//此属性包含日期时间编辑的最短时间。
//更改此属性会更新最小DateTime属性的时间,同时保留最小日期和最大日期属性。
//如果这些日期属性重合,在设置此属性时,如果需要,会调整最大时间属性以确保范围仍然有效。
// 否则,更改此属性会保留最大DateTime属性。
//此属性可以设置为任何有效的 QTime 值。默认情况下,此属性包含时间为00:00:00和0毫秒。
//可以通过clearMinimumTime() 恢复默认值。
//This property holds the current section of the spinbox.即螺旋输入框内的焦点区域
//默认的区域索引是 0,即把输入焦点默认放在最左边的年份区域。枚举值 YearSection = 0x0400
Q_PROPERTY(Section currentSection //枚举类 Section就定义在本类
READ currentSection WRITE setCurrentSection)
Q_PROPERTY(int currentSectionIndex //索引值默认还是从 0开始计数
READ currentSectionIndex WRITE setCurrentSectionIndex)
//This property holds the current section index of the spinbox.
//如果格式为'yyyy/MM/dd',则displayText为'2001/05/21',
// 则如果cursorPosition为5,currentSectionndex返回1。
// 则如果cursorPosition为3,currentSectionIndex为0,依此类推。
Q_PROPERTY(int sectionCount READ sectionCount) //即总共有几节日期时间值
//This property holds the number of sections displayed.
//If the format is 'yyyy/yy/yyyy', sectionCount returns 3
Q_PROPERTY(Sections displayedSections READ displayedSections)
// Sections = QFlags<Section> 这是增加了对枚举运算的支持后的枚举量
//This property holds the currently displayed fields of the date time edit.
//Returns a bit set of the displayed sections for this format.
//This property holds the format used to display the
// time/date of the date time edit.
//This format is described in QDateTime::toString() and QDateTime::fromString()
//请注意,如果您指定两位数年份,它将被解释为和 date time edit的初始化日期时间值在同一个世纪。
//默认世纪是21世纪(2000-2099)。 因为默认的日期是 2000年1月1日。
//如果您指定无效的格式,则不会设置格式。
Q_PROPERTY(QString displayFormat READ displayFormat WRITE setDisplayFormat)
Q_PROPERTY(bool calendarPopup READ calendarPopup WRITE setCalendarPopup)
//This property holds the current calendar pop-up show mode.
//The calendar pop-up will be shown upon clicking the arrow button.
//This property is valid only if there is a valid date display format.
//This property holds the current timespec used by the date time edit.
Q_PROPERTY(Qt::TimeSpec timeSpec READ timeSpec WRITE setTimeSpec)
//enum Qt::TimeSpec { LocalTime, UTC, OffsetFromUTC, TimeZone };
private:
Q_DECLARE_PRIVATE(QDateTimeEdit)
Q_DISABLE_COPY(QDateTimeEdit)
Q_PRIVATE_SLOT(d_func(), void _q_resetButton())
public:
enum Section { // a sub-type of QDateTimeParser's like-named enum.
NoSection = 0x0000,
AmPmSection = 0x0001, //AM PM
MSecSection = 0x0002,
SecondSection = 0x0004,
MinuteSection = 0x0008,
HourSection = 0x0010,
DaySection = 0x0100,
MonthSection = 0x0200,
YearSection = 0x0400,
//= 0000_0001_1110
TimeSections_Mask = AmPmSection|MSecSection|SecondSection|
MinuteSection|HourSection,
//= 0111_0000_0000
DateSections_Mask = DaySection|MonthSection|YearSection
};
Q_ENUM(Section) //将本枚举量接入 Qt的元对象系统
Q_DECLARE_FLAGS(Sections, Section) //定义 Sections = QFlags<Section> 以支持枚举量运算
Q_FLAG(Sections) //这也是完善此枚举类的功能的,以接入元对象系统
/*
#define Q_FLAGS(x) QT_ANNOTATE_CLASS(qt_enums, x)
#define Q_ENUM_IMPL(ENUM) \
friend constexpr const \
QMetaObject * qt_getEnumMetaObject(ENUM) noexcept \
{ return &staticMetaObject; } \
\
friend constexpr const char * \
qt_getEnumName(ENUM) noexcept { return #ENUM; }
#define Q_FLAG(x) Q_FLAGS(x) Q_ENUM_IMPL(x)
*/
//Constructs an empty date time editor with a parent.
explicit QDateTimeEdit(QWidget * parent = nullptr);
explicit QDateTimeEdit(const QDateTime & dt, QWidget * parent = nullptr);
//Constructs an empty date time editor with a parent. The value is set to datetime.
explicit QDateTimeEdit(QDate d, QWidget * parent = nullptr);
//Constructs an empty date time editor with a parent. The value is set to date.
explicit QDateTimeEdit(QTime t, QWidget * parent = nullptr);
//Constructs an empty date time editor with a parent. The value is set to time.
~QDateTimeEdit();
//Q_PROPERTY(QDateTime dateTime READ dateTime WRITE setDateTime
// NOTIFY dateTimeChanged USER true)
QDateTime dateTime() const;
public Q_SLOTS:
void setDateTime (const QDateTime & dateTime);
Q_SIGNALS:
void dateTimeChanged(const QDateTime & dateTime);
public:
// Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY dateChanged)
QDate date () const;
public Q_SLOTS:
void setDate (QDate date);
Q_SIGNALS:
void dateChanged(QDate date);
public:
// Q_PROPERTY(QTime time READ time WRITE setTime NOTIFY timeChanged)
QTime time() const;
public Q_SLOTS:
void setTime (QTime time);
Q_SIGNALS:
void timeChanged(QTime time);
public:
//Q_PROPERTY(QDateTime maximumDateTime
// READ maximumDateTime WRITE setMaximumDateTime
// RESET clearMaximumDateTime) //默认最大值 9999/12/31 23:59:59
QDateTime maximumDateTime() const;
void setMaximumDateTime(const QDateTime & dt);
void clearMaximumDateTime();
//Q_PROPERTY(QDateTime minimumDateTime
// READ minimumDateTime WRITE setMinimumDateTime
// RESET clearMinimumDateTime) //默认最小值 1752/ 9/14 0:00:00
QDateTime minimumDateTime() const;
void setMinimumDateTime(const QDateTime & dt);
void clearMinimumDateTime();
//Q_PROPERTY(QDate maximumDate READ maximumDate WRITE setMaximumDate
// RESET clearMaximumDate)
QDate maximumDate() const;
void setMaximumDate(QDate max);
void clearMaximumDate();
//Q_PROPERTY(QDate minimumDate READ minimumDate WRITE setMinimumDate
// RESET clearMinimumDate)
QDate minimumDate() const;
void setMinimumDate(QDate min);
void clearMinimumDate();
//Q_PROPERTY(QTime maximumTime READ maximumTime WRITE setMaximumTime
// RESET clearMaximumTime) //默认的最大时间值 23:59:59.999
QTime maximumTime() const;
void setMaximumTime(QTime max);
void clearMaximumTime();
//Q_PROPERTY(QTime minimumTime READ minimumTime WRITE setMinimumTime
// RESET clearMinimumTime) //默认的最小时间值 0:00:00.000
QTime minimumTime() const;
void setMinimumTime(QTime min);
void clearMinimumTime();
//Q_PROPERTY(Section currentSection //枚举类 Section就定义在本类
// READ currentSection WRITE setCurrentSection)
Section currentSection() const;
void setCurrentSection(Section section);
//Q_PROPERTY(int currentSectionIndex //索引值默认还是从 0开始计数
// READ currentSectionIndex WRITE setCurrentSectionIndex)
int currentSectionIndex() const;
void setCurrentSectionIndex(int index);
//Q_PROPERTY(int sectionCount READ sectionCount)
int sectionCount() const;
//Returns the Section at index. If the format is 'yyyy/MM/dd',
//sectionAt(0) returns YearSection, sectionAt(1) returns MonthSection。
Section sectionAt (int index ) const;
//Returns the text from the given section.
QString sectionText(Section section) const;
void setSelectedSection (Section section); //但本函数的显示效果最醒目
//结论:setSelectedSection() = setCurrentSectionIndex() = setCurrentSection()
//Selects section. If section doesn't exist in the
// currently displayed sections,
//this function does nothing. If section is NoSection,
//this function will unselect all text in the editor.
//Otherwise, this function will move the cursor and the
// current section to the selected section.
// Sections = QFlags<Section>
//Q_PROPERTY(Sections displayedSections READ displayedSections)
Sections displayedSections() const;
//Q_PROPERTY(QString displayFormat READ displayFormat WRITE setDisplayFormat)
QString displayFormat() const;
void setDisplayFormat(const QString & format);
//Q_PROPERTY(bool calendarPopup READ calendarPopup WRITE setCalendarPopup)
bool calendarPopup() const;
void setCalendarPopup(bool enable);
QCalendar calendar() const; //无注释
void setCalendar(QCalendar calendar);
//如果calendarPopup设置为true并且(sections() & DateSections_Mask)!=0,
//则返回编辑器的日历小部件。如果尚未设置日历,则此函数将创建并返回一个日历小部件。
QCalendarWidget * calendarWidget() const;
void setCalendarWidget(QCalendarWidget * calendarWidget);
//Sets the given calendarWidget as the widget to be used for the
// calendar pop-up.
//The editor does not automatically take ownership of the calendar widget.
//Note: calendarPopup must be set to true before setting the calendar widget.
//enum Qt::TimeSpec { LocalTime, UTC, OffsetFromUTC, TimeZone };
//Q_PROPERTY(Qt::TimeSpec timeSpec READ timeSpec WRITE setTimeSpec)
Qt::TimeSpec timeSpec() const;
void setTimeSpec(Qt::TimeSpec spec);
QSize sizeHint() const override;
void setDateTimeRange(const QDateTime & min, const QDateTime & max);
void setDateRange(QDate min, QDate max);
void setTimeRange(QTime min, QTime max);
void clear() override; //void QAbstractSpinBox::clear()
//Clears the lineedit of all text but prefix and suffix. 重写了继承来的函数
void stepBy(int steps) override; //void QAbstractSpinBox::stepBy(int steps)
//举例:按箭头则形参 steps = 1,按 pageUp 则 steps = 10;
protected:
StepEnabled stepEnabled() const override; //表明当前的步进操作是否被允许
//Reimplements: QAbstractSpinBox::stepEnabled() const.
/*
enum StepEnabledFlag { StepNone, StepUpEnabled, StepDownEnabled };
StepEnabled = QFlags<StepEnabledFlag>
virtual StepEnabled stepEnabled() const;
//虚拟函数,用于确定在任何给定时间上下移动是否合法。
//上箭头将被绘制为禁用,除非(stepEnabled()&StepUpEnabled)!=0。
//如果启用包裹,默认实现将返回(StepUpEnabledlStepDownEnabled)。
//否则,如果值大于最小值,它将返回StepDownEnabled;
//如果值小于最大值,则与StepUpEnabled“或”操作。
//如果你子类化 QAbstractSpinBox,你需要重新实现这个函数。
*/
QValidator::State validate(QString & input, int & pos) const override;
/* QAbstractSpinBox::validate(QString & input, int & pos) const. 继承来的
//class QValidator : public QObject { // QT 中所有类都继承自 QObject
// enum State { Invalid, Intermediate, Acceptable }; };
virtual QValidator::State validate(QString & input, int & pos) const;
//This virtual function is called by the QAbstractSpinBox to determine
//whether input is valid.
//The pos parameter indicates the position in the string.
//Reimplemented in the various subclasses.
*/
void fixup(QString & input) const override;
/* 继承 QAbstractSpinBox::fixup(QString & input) const.
virtual void fixup(QString & input) const; //虚函数,可由子类重新实现
//This virtual function is called by the QAbstractSpinBox
//if the input is not validated to QValidator::Acceptable //此枚举值在上面的注释里
//when Return is pressed or interpretText() is called.
//It will try to change the text so it is valid.
//Reimplemented in the various subclasses.
*/
//Returns an appropriate datetime for the given text.
//This virtual function is used by the datetime edit whenever it needs to
// interpret text entered by the user as a value.
virtual QDateTime dateTimeFromText(const QString & text) const;
virtual QString textFromDateTime(const QDateTime & dt) const;
//This virtual function is used by the date time edit whenever it needs to
// display dateTime.
//If you reimplement this, you may also need to reimplement validate().
void initStyleOption(QStyleOptionSpinBox *option) const override;
QDateTimeEdit(const QVariant & val, //无注释
QMetaType::Type parserType, QWidget * parent = nullptr);
public:
bool event(QEvent * event) override;
protected:
void paintEvent(QPaintEvent * event) override;
void mousePressEvent(QMouseEvent * event) override;
void wheelEvent(QWheelEvent * event) override;
void keyPressEvent(QKeyEvent * event) override;
void focusInEvent(QFocusEvent * event) override;
bool focusNextPrevChild(bool next) override;
//Reimplements: QWidget::focusNextPrevChild(bool next).
//Finds a new widget to give the keyboard focus to,
// as appropriate for Tab and Shift+Tab,
//and returns true if it can find a new widget, or false if it can't.
//If next is true , this function searches forward,
//if next is false, it searches backward.
Q_SIGNALS:
//void dateTimeChanged(const QDateTime & dateTime);
//void timeChanged(QTime time);
//void dateChanged(QDate date);
public Q_SLOTS:
//void setDateTime(const QDateTime & dateTime);
//void setDate (QDate date);
//void setTime (QTime time);
}; //完结 class QDateTimeEdit : public QAbstractSpinBox
/*
The QTimeEdit class provides a widget for editing times based on the
QDateTimeEdit widget.
QTimeEdit提供的许多属性和功能在QDateTimeEdit中实现。这些是该类的相关属性:
time 持有由小部件显示的时间。
minimumTime 定义了用户可以设置的最低(最早)时间。
maximumTime 定义了用户可以设置的最多(最新)时间。
displayFormat 包含一个字符串,用于格式化小部件中显示的时间。
*/
class Q_WIDGETS_EXPORT QTimeEdit : public QDateTimeEdit //子类
{
Q_OBJECT
Q_PROPERTY(QTime time READ time WRITE setTime NOTIFY userTimeChanged USER true)
// QDateTimeEdit { Q_PROPERTY(QTime time READ time WRITE setTime NOTIFY timeChanged) }
Q_SIGNALS:
void userTimeChanged(QTime time); //void QDateTimeEdit::timeChanged(QTime time);
public:
explicit QTimeEdit(QWidget * parent = nullptr);
explicit QTimeEdit(QTime time, QWidget * parent = nullptr);
~QTimeEdit();
}; //完结 class QTimeEdit : public QDateTimeEdit
class Q_WIDGETS_EXPORT QDateEdit : public QDateTimeEdit
{
Q_OBJECT //总之,这俩子类新增的信号函数,只是换了个函数名称,作为信号函数的功能上没有任何变化。
Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY userDateChanged USER true)
// QDateTimeEdit { Q_PROPERTY(QDate date READ date WRITE setDate NOTIFY dateChanged) }
Q_SIGNALS:
void userDateChanged(QDate date); //void QDateTimeEdit::dateChanged(QDate date);
public:
explicit QDateEdit(QWidget * parent = nullptr);
explicit QDateEdit(QDate date, QWidget * parent = nullptr);
~QDateEdit();
}; //完结 class QDateEdit : public QDateTimeEdit
Q_DECLARE_OPERATORS_FOR_FLAGS(QDateTimeEdit::Sections) //为此枚举类增加运算函数
QT_END_NAMESPACE
#endif // QDATETIMEEDIT_H
(7)
谢谢