QDateTimeEdit Class

QDateTimeEdit 提供一个日期和时间编辑widget. 更多...

头文件: #include <QDateTimeEdit>
qmake: QT += widgets
基类: QAbstractSpinBox
派生类:

QDateEdit and QTimeEdit

公有类型

enum Section { NoSection, AmPmSection, MSecSection, SecondSection, ..., YearSection }
flags Sections

属性

公有函数

QDateTimeEdit(QWidget *parent = Q_NULLPTR)
QDateTimeEdit(const QDateTime &datetime, QWidget *parent = Q_NULLPTR)
QDateTimeEdit(const QDate &date, QWidget *parent = Q_NULLPTR)
QDateTimeEdit(const QTime &time, QWidget *parent = Q_NULLPTR)
~QDateTimeEdit()
bool calendarPopup() const
QCalendarWidget *calendarWidget() const
void clearMaximumDate()
void clearMaximumDateTime()
void clearMaximumTime()
void clearMinimumDate()
void clearMinimumDateTime()
void clearMinimumTime()
Section currentSection() const
int currentSectionIndex() const
QDate date() const
QDateTime dateTime() const
QString displayFormat() const
Sections displayedSections() const
QDate maximumDate() const
QDateTime maximumDateTime() const
QTime maximumTime() const
QDate minimumDate() const
QDateTime minimumDateTime() const
QTime minimumTime() const
Section sectionAt(int index) const
int sectionCount() const
QString sectionText(Section section) const
void setCalendarPopup(bool enable)
void setCalendarWidget(QCalendarWidget *calendarWidget)
void setCurrentSection(Section section)
void setCurrentSectionIndex(int index)
void setDateRange(const QDate &min, const QDate &max)
void setDateTimeRange(const QDateTime &min, const QDateTime &max)
void setDisplayFormat(const QString &format)
void setMaximumDate(const QDate &max)
void setMaximumDateTime(const QDateTime &dt)
void setMaximumTime(const QTime &max)
void setMinimumDate(const QDate &min)
void setMinimumDateTime(const QDateTime &dt)
void setMinimumTime(const QTime &min)
void setSelectedSection(Section section)
void setTimeRange(const QTime &min, const QTime &max)
void setTimeSpec(Qt::TimeSpec spec)
QTime time() const
Qt::TimeSpec timeSpec() const

重新实现的公有函数

virtual void clear() override
virtual bool event(QEvent *event) override
virtual QSize sizeHint() const override
virtual void stepBy(int steps) override

公有槽函数

void setDate(const QDate &date)
void setDateTime(const QDateTime &dateTime)
void setTime(const QTime &time)

信号

void dateChanged(const QDate &date)
void dateTimeChanged(const QDateTime &datetime)
void timeChanged(const QTime &time)

受保护的函数

virtual QDateTime dateTimeFromText(const QString &text) const
void initStyleOption(QStyleOptionSpinBox *option) const
virtual QString textFromDateTime(const QDateTime &dateTime) const

重新实现的受保护函数

virtual void fixup(QString &input) const override
virtual void focusInEvent(QFocusEvent *event) override
virtual bool focusNextPrevChild(bool next) override
virtual void keyPressEvent(QKeyEvent *event) override
virtual void mousePressEvent(QMouseEvent *event) override
virtual void paintEvent(QPaintEvent *event) override
virtual StepEnabled stepEnabled() const override
virtual QValidator::State validate(QString &text, int &pos) const override
virtual void wheelEvent(QWheelEvent *event) override

其他继承的成员

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

详细描述

QDateTimeEdit 提供一个日期和时间编辑widget.

QDateTimeEdit 允许用户使用键盘或方向键修改日期和时间. 方向键可以在 QDateTimeEdit 的不同部分切换. 日期和时间按照 setDisplayFormat() 函数设置的格式显示.


  QDateTimeEdit *dateEdit = new QDateTimeEdit(QDate::currentDate());
  dateEdit->setMinimumDate(QDate::currentDate().addDays(-365));
  dateEdit->setMaximumDate(QDate::currentDate().addDays(365));
  dateEdit->setDisplayFormat("yyyy.MM.dd");

上述代码使用当前日期创建一个 QDateTimeEdit 对象, 并将有效时间的范围限制为加减365天. 我们定义时间格式是年,月,日.

QDateTimeEdit 最小日期是1752年9月14日. 你可以调用 setMinimumDate()修改最小值; 注意: QDate 的最小值是公元前4713年1月2日.

另一些有用函数是 setMaximumDate(), setMinimumTime() 和 setMaximumTime().

弹出式日历widget

QDateTimeEdit 可以配置一个 QCalendarWidget 修改日期. 这个功能通过设置 calendarPopup 属性开启. 此外, 你可以调用 setCalendarWidget() 函数设置自定义日历widget. 调用 calendarWidget()获取当前日历widget.

Screenshot of a Windows Vista style date time editing widgetA date time editing widget shown in the Windows Vista widget style.
Screenshot of a Macintosh style date time editing widgetA date time editing widget shown in the Macintosh widget style.
Screenshot of a Fusion style date time editing widgetA date time editing widget shown in the Fusion widget style.

参见 QDateEdit, QTimeEdit, QDate, QTime.

成员类型

enum QDateTimeEdit::Section
flags QDateTimeEdit::Sections

ConstantValue
QDateTimeEdit::NoSection0x0000
QDateTimeEdit::AmPmSection0x0001
QDateTimeEdit::MSecSection0x0002
QDateTimeEdit::SecondSection0x0004
QDateTimeEdit::MinuteSection0x0008
QDateTimeEdit::HourSection0x0010
QDateTimeEdit::DaySection0x0100
QDateTimeEdit::MonthSection0x0200
QDateTimeEdit::YearSection0x0400

The Sections type is a typedef for QFlags<Section>. It stores an OR combination of Section values.

属性

calendarPopup : bool

当前日历弹出模式是否生效.

单击下拉按钮, 弹出日历界面. 这个属性仅在设置有效的日期格式才生效.

This property was introduced in Qt 4.2.

访问函数:

bool calendarPopup() const
void setCalendarPopup(bool enable)

参见 setDisplayFormat().

currentSection : Section

This property holds the current section of the spinbox setCurrentSection()

访问函数:

Section currentSection() const
void setCurrentSection(Section section)

currentSectionIndex : int

This property holds the current section index of the spinbox

If the format is 'yyyy/MM/dd', the displayText is '2001/05/21', and the cursorPosition is 5, currentSectionIndex returns 1. If the cursorPosition is 3, currentSectionIndex is 0, and so on.

setCurrentSection()

This property was introduced in Qt 4.3.

访问函数:

int currentSectionIndex() const
void setCurrentSectionIndex(int index)

参见 currentSection().

date : QDate

This property holds the QDate that is set in the widget

By default, this property contains a date that refers to January 1, 2000.

访问函数:

QDate date() const
void setDate(const QDate &date)

Notifier signal:

void dateChanged(const QDate &date)

参见 time and dateTime.

dateTime : QDateTime

This property holds the QDateTime that is set in the QDateTimeEdit

When setting this property the timespec of the QDateTimeEdit remains the same and the timespec of the new QDateTime is ignored.

By default, this property contains a date that refers to January 1, 2000 and a time of 00:00:00 and 0 milliseconds.

访问函数:

QDateTime dateTime() const
void setDateTime(const QDateTime &dateTime)

Notifier signal:

void dateTimeChanged(const QDateTime &datetime)

参见 date and time.

displayFormat : QString

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

Example format strings (assuming that the date is 2nd of July 1969):

FormatResult
dd.MM.yyyy02.07.1969
MMM d yyJul 2 69
MMMM d yyJuly 2 69

Note that if you specify a two digit year, it will be interpreted to be in the century in which the date time edit was initialized. The default century is the 21 (2000-2099).

If you specify an invalid format the format will not be set.

访问函数:

QString displayFormat() const
void setDisplayFormat(const QString &format)

参见 QDateTime::toString() and displayedSections().

displayedSections : const Sections

This property holds the currently displayed fields of the date time edit

Returns a bit set of the displayed sections for this format. setDisplayFormat(), displayFormat()

访问函数:

Sections displayedSections() const

maximumDate : QDate

This property holds the maximum date of the date time edit

When setting this property the minimumDate is adjusted if necessary to ensure that the range remains valid. If the date is not a valid QDate object, this function does nothing.

By default, this property contains a date that refers to December 31, 7999.

访问函数:

QDate maximumDate() const
void setMaximumDate(const QDate &max)
void clearMaximumDate()

参见 minimumDate, minimumTime, maximumTime, and setDateRange().

maximumDateTime : QDateTime

This property holds the maximum datetime of the date time edit

When setting this property the minimumDateTime() is adjusted if necessary to ensure that the range remains valid. If the datetime is not a valid QDateTime object, this function does nothing.

The default maximumDateTime can be restored with clearMaximumDateTime().

By default, this property contains a date that refers to 31 December, 7999 and a time of 23:59:59 and 999 milliseconds.

This property was introduced in Qt 4.4.

访问函数:

QDateTime maximumDateTime() const
void setMaximumDateTime(const QDateTime &dt)
void clearMaximumDateTime()

参见 minimumDateTime(), minimumTime(), maximumTime(), minimumDate(), maximumDate(), setDateTimeRange(), setDateRange(), setTimeRange(), clearMinimumDateTime(), clearMinimumDate(), clearMaximumDate(), clearMinimumTime(), and clearMaximumTime().

maximumTime : QTime

This property holds the maximum time of the date time edit

When setting this property, the minimumTime is adjusted if necessary to ensure that the range remains valid. If the time is not a valid QTime object, this function does nothing.

By default, this property contains a time of 23:59:59 and 999 milliseconds.

访问函数:

QTime maximumTime() const
void setMaximumTime(const QTime &max)
void clearMaximumTime()

参见 minimumTime, minimumDate, maximumDate, and setTimeRange().

minimumDate : QDate

This property holds the minimum date of the date time edit

When setting this property the maximumDate is adjusted if necessary, to ensure that the range remains valid. If the date is not a valid QDate object, this function does nothing.

By default, this property contains a date that refers to September 14, 1752. The minimum date must be at least the first day in year 100, otherwise setMinimumDate() has no effect.

访问函数:

QDate minimumDate() const
void setMinimumDate(const QDate &min)
void clearMinimumDate()

参见 minimumTime(), maximumTime(), and setDateRange().

minimumDateTime : QDateTime

This property holds the minimum datetime of the date time edit

When setting this property the maximumDateTime() is adjusted if necessary to ensure that the range remains valid. If the datetime is not a valid QDateTime object, this function does nothing.

The default minimumDateTime can be restored with clearMinimumDateTime()

By default, this property contains a date that refers to September 14, 1752 and a time of 00:00:00 and 0 milliseconds.

This property was introduced in Qt 4.4.

访问函数:

QDateTime minimumDateTime() const
void setMinimumDateTime(const QDateTime &dt)
void clearMinimumDateTime()

参见 maximumDateTime(), minimumTime(), maximumTime(), minimumDate(), maximumDate(), setDateTimeRange(), setDateRange(), setTimeRange(), clearMaximumDateTime(), clearMinimumDate(), clearMaximumDate(), clearMinimumTime(), and clearMaximumTime().

minimumTime : QTime

This property holds the minimum time of the date time edit

When setting this property the maximumTime is adjusted if necessary, to ensure that the range remains valid. If the time is not a valid QTime object, this function does nothing.

By default, this property contains a time of 00:00:00 and 0 milliseconds.

访问函数:

QTime minimumTime() const
void setMinimumTime(const QTime &min)
void clearMinimumTime()

参见 maximumTime, minimumDate, maximumDate, and setTimeRange().

sectionCount : const int

This property holds the number of sections displayed. If the format is 'yyyy/yy/yyyy', sectionCount returns 3

This property was introduced in Qt 4.3.

访问函数:

int sectionCount() const

time : QTime

This property holds the QTime that is set in the widget

By default, this property contains a time of 00:00:00 and 0 milliseconds.

访问函数:

QTime time() const
void setTime(const QTime &time)

Notifier signal:

void timeChanged(const QTime &time)

参见 date and dateTime.

timeSpec : Qt::TimeSpec

This property holds the current timespec used by the date time edit.

This property was introduced in Qt 4.4.

访问函数:

Qt::TimeSpec timeSpec() const
void setTimeSpec(Qt::TimeSpec spec)

成员函数

QDateTimeEdit::QDateTimeEdit(QWidget *parent = Q_NULLPTR)

Constructs an empty date time editor with a parent.

QDateTimeEdit::QDateTimeEdit(const QDateTime &datetime, QWidget *parent = Q_NULLPTR)

Constructs an empty date time editor with a parent. The value is set to datetime.

QDateTimeEdit::QDateTimeEdit(const QDate &date, QWidget *parent = Q_NULLPTR)

Constructs an empty date time editor with a parent. The value is set to date.

QDateTimeEdit::QDateTimeEdit(const QTime &time, QWidget *parent = Q_NULLPTR)

Constructs an empty date time editor with a parent. The value is set to time.

QDateTimeEdit::~QDateTimeEdit()

Destructor.

QCalendarWidget *QDateTimeEdit::calendarWidget() const

Returns the calendar widget for the editor if calendarPopup is set to true and (sections() & DateSections_Mask) != 0.

This function creates and returns a calendar widget if none has been set.

This function was introduced in Qt 4.4.

参见 setCalendarWidget().

[override virtual] void QDateTimeEdit::clear()

重新实现 QAbstractSpinBox::clear().

QDate QDateTimeEdit::date() const

Returns the date of the date time edit.

Note: Getter function for property date.

参见 setDate().

[signal] void QDateTimeEdit::dateChanged(const QDate &date)

This signal is emitted whenever the date is changed. The new date is passed in date.

Note: Notifier signal for property date.

[signal] void QDateTimeEdit::dateTimeChanged(const QDateTime &datetime)

This signal is emitted whenever the date or time is changed. The new date and time is passed in datetime.

Note: Notifier signal for property dateTime.

[virtual protected] QDateTime QDateTimeEdit::dateTimeFromText(const QString &text) const

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.

参见 textFromDateTime() and validate().

[override virtual] bool QDateTimeEdit::event(QEvent *event)

重新实现 QObject::event().

[override virtual protected] void QDateTimeEdit::fixup(QString &input) const

重新实现 QAbstractSpinBox::fixup().

[override virtual protected] void QDateTimeEdit::focusInEvent(QFocusEvent *event)

重新实现 QWidget::focusInEvent().

[override virtual protected] bool QDateTimeEdit::focusNextPrevChild(bool next)

重新实现 QWidget::focusNextPrevChild().

[protected] void QDateTimeEdit::initStyleOption(QStyleOptionSpinBox *option) const

Initialize option with the values from this QDataTimeEdit. This method is useful for subclasses when they need a QStyleOptionSpinBox, but don't want to fill in all the information themselves.

参见 QStyleOption::initFrom().

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

重新实现 QWidget::keyPressEvent().

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

重新实现 QWidget::mousePressEvent().

[override virtual protected] void QDateTimeEdit::paintEvent(QPaintEvent *event)

重新实现 QWidget::paintEvent().

Section QDateTimeEdit::sectionAt(int index) const

Returns the Section at index.

If the format is 'yyyy/MM/dd', sectionAt(0) returns YearSection, sectionAt(1) returns MonthSection, and sectionAt(2) returns YearSection,

This function was introduced in Qt 4.3.

QString QDateTimeEdit::sectionText(Section section) const

Returns the text from the given section.

参见 currentSection().

void QDateTimeEdit::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.

This function was introduced in Qt 4.4.

参见 calendarWidget() and calendarPopup.

void QDateTimeEdit::setDateRange(const QDate &min, const QDate &max)

Convenience function to set minimum and maximum date with one function call.


  setDateRange(min, max);

is analogous to:


  setMinimumDate(min);
  setMaximumDate(max);

If either min or max are not valid, this function does nothing.

参见 setMinimumDate(), maximumDate(), setMaximumDate(), clearMinimumDate(), setMinimumTime(), maximumTime(), setMaximumTime(), clearMinimumTime(), and QDate::isValid().

void QDateTimeEdit::setDateTimeRange(const QDateTime &min, const QDateTime &max)

Convenience function to set minimum and maximum date time with one function call.


  setDateTimeRange(min, max);

is analogous to:


  setMinimumDateTime(min);
  setMaximumDateTime(max);

If either min or max are not valid, this function does nothing.

This function was introduced in Qt 4.4.

参见 setMinimumDate(), maximumDate(), setMaximumDate(), clearMinimumDate(), setMinimumTime(), maximumTime(), setMaximumTime(), clearMinimumTime(), and QDateTime::isValid().

void QDateTimeEdit::setSelectedSection(Section section)

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.

This function was introduced in Qt 4.2.

参见 currentSection().

void QDateTimeEdit::setTimeRange(const QTime &min, const QTime &max)

Convenience function to set minimum and maximum time with one function call.


  setTimeRange(min, max);

is analogous to:


  setMinimumTime(min);
  setMaximumTime(max);

If either min or max are not valid, this function does nothing.

参见 setMinimumDate(), maximumDate(), setMaximumDate(), clearMinimumDate(), setMinimumTime(), maximumTime(), setMaximumTime(), clearMinimumTime(), and QTime::isValid().

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

重新实现 QWidget::sizeHint().

[override virtual] void QDateTimeEdit::stepBy(int steps)

重新实现 QAbstractSpinBox::stepBy().

[override virtual protected] StepEnabled QDateTimeEdit::stepEnabled() const

重新实现 QAbstractSpinBox::stepEnabled().

[virtual protected] QString QDateTimeEdit::textFromDateTime(const QDateTime &dateTime) 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().

参见 dateTimeFromText() and validate().

QTime QDateTimeEdit::time() const

Returns the time of the date time edit.

Note: Getter function for property time.

参见 setTime().

[signal] void QDateTimeEdit::timeChanged(const QTime &time)

This signal is emitted whenever the time is changed. The new time is passed in time.

Note: Notifier signal for property time.

[override virtual protected] QValidator::State QDateTimeEdit::validate(QString &text, int &pos) const

重新实现 QAbstractSpinBox::validate().

[override virtual protected] void QDateTimeEdit::wheelEvent(QWheelEvent *event)

重新实现 QWidget::wheelEvent().