QDateTimeEdit 类

QDateTimeEdit 类提供用于编辑日期和时间的 Widget。 更多...

头: #include <QDateTimeEdit>
qmake: QT += widgets
继承: QAbstractSpinBox
继承者:

QDateEdit and QTimeEdit

公共类型

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

特性

公共函数

QDateTimeEdit (const QTime & time , QWidget * parent = nullptr)
QDateTimeEdit (const QDate & date , QWidget * parent = nullptr)
QDateTimeEdit (const QDateTime & datetime , QWidget * parent = nullptr)
QDateTimeEdit (QWidget * parent = nullptr)
virtual ~QDateTimeEdit ()
bool calendarPopup () const
QCalendarWidget * calendarWidget () const
void clearMaximumDate ()
void clearMaximumDateTime ()
void clearMaximumTime ()
void clearMinimumDate ()
void clearMinimumDateTime ()
void clearMinimumTime ()
QDateTimeEdit::Section currentSection () const
int currentSectionIndex () const
QDate date () const
QDateTime dateTime () const
QString displayFormat () const
QDateTimeEdit::Sections displayedSections () const
QDate maximumDate () const
QDateTime maximumDateTime () const
QTime maximumTime () const
QDate minimumDate () const
QDateTime minimumDateTime () const
QTime minimumTime () const
QDateTimeEdit::Section sectionAt (int index ) const
int sectionCount () const
QString sectionText (QDateTimeEdit::Section section ) const
void setCalendarPopup (bool enable )
void setCalendarWidget (QCalendarWidget * calendarWidget )
void setCurrentSection (QDateTimeEdit::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 (QDateTimeEdit::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 QAbstractSpinBox::StepEnabled stepEnabled () const override
virtual QValidator::State validate (QString & text , int & pos ) const override
virtual void wheelEvent (QWheelEvent * event ) override

详细描述

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

Here we've created a new QDateTimeEdit object initialized with today's date, and restricted the valid date range to today plus or minus 365 days. We've set the order to month, day, year.

The range of valid values for a QDateTimeEdit is controlled by the properties minimumDateTime , maximumDateTime , and their respective date and time components. By default, any date-time from the start of 100 CE to the end of 9999 CE is valid.

使用弹出日历 Widget

QDateTimeEdit can be configured to allow a QCalendarWidget to be used to select dates. This is enabled by setting the calendarPopup property. Additionally, you can supply a custom calendar widget for use as the calendar pop-up by calling the setCalendarWidget () function. The existing calendar widget can be retrieved with calendarWidget ().

键盘追踪

键盘追踪 is enabled (the default), every keystroke of editing a field triggers signals for value changes.

When the allowed range is narrower than some time interval whose end it straddles, keyboard tracking prevents the user editing the date or time to access the later part of the interval. For example, for a range from 29.04.2020 to 02.05.2020 and an initial date of 30.04.2020, the user can change neither the month (May 30th is outside the range) nor the day (April 2nd is outside the range).

When keyboard tracking is disabled, changes are only signalled when focus leaves the text field after edits have modified the content. This allows the user to edit via an invalid date-time to reach a valid one.

另请参阅 QDateEdit , QTimeEdit , QDate ,和 QTime .

成员类型文档编制

enum QDateTimeEdit:: Section
flags QDateTimeEdit:: Sections

常量
QDateTimeEdit::NoSection 0x0000
QDateTimeEdit::AmPmSection 0x0001
QDateTimeEdit::MSecSection 0x0002
QDateTimeEdit::SecondSection 0x0004
QDateTimeEdit::MinuteSection 0x0008
QDateTimeEdit::HourSection 0x0010
QDateTimeEdit::DaySection 0x0100
QDateTimeEdit::MonthSection 0x0200
QDateTimeEdit::YearSection 0x0400

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

特性文档编制

calendarPopup : bool

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.

该特性在 Qt 4.2 引入。

访问函数:

bool calendarPopup () const
void setCalendarPopup (bool enable )

另请参阅 setDisplayFormat ().

currentSection : Section

This property holds the current section of the spinbox.

访问函数:

QDateTimeEdit::Section currentSection () const
void setCurrentSection (QDateTimeEdit::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.

该特性在 Qt 4.3 引入。

访问函数:

int currentSectionIndex () const
void setCurrentSectionIndex (int index )

另请参阅 setCurrentSection () 和 currentSection ().

date : QDate

此特性保持 QDate 是在 Widget 设置的。

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

访问函数:

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

通知程序信号:

void dateChanged (const QDate & date )

另请参阅 time and dateTime .

dateTime : QDateTime

此特性保持 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 被忽略。

By default, this property is set to the start of 2000 CE. It can only be set to a valid QDateTime value. If any operation causes this property to have an invalid date-time as value, it is reset to the value of the minimumDateTime 特性。

QDateTimeEdit has no date fields, setting this property sets the widget's date-range to start and end on the date of the new value of this property.

访问函数:

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

通知程序信号:

void dateTimeChanged (const QDateTime & datetime )

另请参阅 date , time , minimumDateTime ,和 maximumDateTime .

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 () 和 QDateTime::fromString ()

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

格式 结果
dd.MM.yyyy 02.07.1969
MMM d yy Jul 2 69
MMMM d yy July 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 21st (2000-2099).

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

访问函数:

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

另请参阅 QDateTime::toString () 和 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.

访问函数:

QDateTimeEdit::Sections displayedSections () const

另请参阅 setDisplayFormat () 和 displayFormat ().

maximumDate : QDate

This property holds the maximum date of the date time edit.

Changing this property updates the date of the maximumDateTime property while preserving the maximumTime property. When setting this property, the minimumDate is adjusted, if necessary, to ensure that the range remains valid. When this happens, the minimumTime property is also adjusted if it is greater than the maximumTime property. Otherwise, changes to this property preserve the minimumDateTime 特性。

This property can only be set to a valid QDate object describing a date on which the current maximumTime property makes a valid QDateTime object. The latest date that setMaximumDate() accepts is the end of 9999 CE. This is the default for this property. This default can be restored with clearMaximumDateTime ().

访问函数:

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

另请参阅 minimumDate , maximumTime , maximumDateTime , setDateRange (), QDate::isValid (),和 键盘追踪 .

maximumDateTime : QDateTime

This property holds the maximum datetime of the date time edit.

Changing this property implicitly updates the maximumDate and maximumTime properties to the date and time parts of this property, respectively. When setting this property, the minimumDateTime is adjusted, if necessary, to ensure that the range remains valid. Otherwise, changing this property preserves the minimumDateTime 特性。

This property can only be set to a valid QDateTime value. The latest date-time that setMaximumDateTime() accepts is the end of 9999 CE. This is the default for this property. This default can be restored with clearMaximumDateTime().

该特性在 Qt 4.4 引入。

访问函数:

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

另请参阅 minimumDateTime , maximumTime , maximumDate (), setDateTimeRange (), QDateTime::isValid (),和 键盘追踪 .

maximumTime : QTime

This property holds the maximum time of the date time edit.

Changing this property updates the time of the maximumDateTime property while preserving the minimumDate and maximumDate properties. If those date properties coincide, when setting this property, the minimumTime property is adjusted, if necessary, to ensure that the range remains valid. Otherwise, changing this property preserves the minimumDateTime 特性。

This property can be set to any valid QTime value. By default, this property contains a time of 23:59:59 and 999 milliseconds. This default can be restored with clearMaximumTime().

访问函数:

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

另请参阅 minimumTime , maximumDate , maximumDateTime , setTimeRange (), QTime::isValid (),和 键盘追踪 .

minimumDate : QDate

This property holds the minimum date of the date time edit.

Changing this property updates the date of the minimumDateTime property while preserving the minimumTime property. When setting this property, the maximumDate is adjusted, if necessary, to ensure that the range remains valid. When this happens, the maximumTime property is also adjusted if it is less than the minimumTime property. Otherwise, changes to this property preserve the maximumDateTime 特性。

This property can only be set to a valid QDate object describing a date on which the current minimumTime property makes a valid QDateTime object. The earliest date that setMinimumDate() accepts is the start of 100 CE. The default for this property is September 14, 1752 CE. This default can be restored with clearMinimumDateTime ().

访问函数:

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

另请参阅 maximumDate , minimumTime , minimumDateTime , setDateRange (), QDate::isValid (),和 键盘追踪 .

minimumDateTime : QDateTime

This property holds the minimum datetime of the date time edit.

Changing this property implicitly updates the minimumDate and minimumTime properties to the date and time parts of this property, respectively. When setting this property, the maximumDateTime is adjusted, if necessary, to ensure that the range remains valid. Otherwise, changing this property preserves the minimumDateTime property.

This property can only be set to a valid QDateTime value. The earliest date-time that setMinimumDateTime() accepts is the start of 100 CE. The property's default is the start of September 14, 1752 CE. This default can be restored with clearMinimumDateTime().

该特性在 Qt 4.4 引入。

访问函数:

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

另请参阅 maximumDateTime , minimumTime , minimumDate , setDateTimeRange (), QDateTime::isValid (),和 键盘追踪 .

minimumTime : QTime

This property holds the minimum time of the date time edit.

Changing this property updates the time of the minimumDateTime property while preserving the minimumDate and maximumDate properties. If those date properties coincide, when setting this property, the maximumTime property is adjusted, if necessary, to ensure that the range remains valid. Otherwise, changing this property preserves the maximumDateTime 特性。

This property can be set to any valid QTime value. By default, this property contains a time of 00:00:00 and 0 milliseconds. This default can be restored with clearMinimumTime().

访问函数:

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

另请参阅 maximumTime , minimumDate , minimumDateTime , setTimeRange (), QTime::isValid (),和 键盘追踪 .

sectionCount : const int

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

该特性在 Qt 4.3 引入。

访问函数:

int sectionCount () const

time : QTime

此特性保持 QTime 是在 Widget 设置的。

默认情况下,此特性包含时间 00:00:00 和 0 毫秒。

访问函数:

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

通知程序信号:

void timeChanged (const QTime & time )

另请参阅 date and dateTime .

timeSpec : Qt::TimeSpec

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

该特性在 Qt 4.4 引入。

访问函数:

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

成员函数文档编制

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

构造空的日期时间编辑器采用 parent . The value is set to time .

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

构造空的日期时间编辑器采用 parent . The value is set to date .

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

构造空的日期时间编辑器采用 parent . The value is set to datetime .

QDateTimeEdit:: QDateTimeEdit ( QWidget * parent = nullptr)

构造空的日期时间编辑器采用 parent .

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

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

注意: 通知程序信号对于特性 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 .

注意: 通知程序信号对于特性 dateTime .

另请参阅 键盘追踪 .

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

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

注意: 通知程序信号对于特性 time .

另请参阅 键盘追踪 .

[virtual] QDateTimeEdit:: ~QDateTimeEdit ()

析构函数。

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.

该函数在 Qt 4.4 引入。

另请参阅 setCalendarWidget ().

[override virtual] void QDateTimeEdit:: clear ()

重实现: QAbstractSpinBox::clear ().

QDate QDateTimeEdit:: date () const

返回日期时间编辑的日期。

注意: Getter 函数对于特性 date。

另请参阅 setDate ().

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

返回适当日期时间为给定 text .

This virtual function is used by the datetime edit whenever it needs to interpret text entered by the user as a value.

另请参阅 textFromDateTime () 和 validate ().

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

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

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

重实现: QAbstractSpinBox::fixup (QString &input) const.

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

重实现: QAbstractSpinBox::focusInEvent (QFocusEvent *event).

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

重实现: QWidget::focusNextPrevChild (bool next).

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

初始化 option with the values from this QDataTimeEdit. This method is useful for subclasses when they need a QStyleOptionSpinBox ,但不希望自己填充所有信息。

另请参阅 QStyleOption::initFrom ().

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

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

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

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

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

重实现: QAbstractSpinBox::paintEvent (QPaintEvent *event).

QDateTimeEdit::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 ,

该函数在 Qt 4.3 引入。

QString QDateTimeEdit:: sectionText ( QDateTimeEdit::Section section ) const

Returns the text from the given section .

另请参阅 currentSection ().

void QDateTimeEdit:: setCalendarWidget ( QCalendarWidget * calendarWidget )

设置给定 calendarWidget as the widget to be used for the calendar pop-up. The editor does not automatically take ownership of the calendar widget.

注意: calendarPopup must be set to true before setting the calendar widget.

该函数在 Qt 4.4 引入。

另请参阅 calendarWidget () 和 calendarPopup .

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

Set the range of allowed dates for the date time edit.

This convenience function sets the minimumDate and maximumDate 特性。

setDateRange(min, max);
					

is analogous to:

setMinimumDate(min);
setMaximumDate(max);
					

If either min or max is invalid, this function does nothing. This function preserves the minimumTime property. If max is less than min , the new maximumDateTime property shall be the new minimumDateTime property. If max 等于 min maximumTime property was less then the minimumTime property, the maximumTime property is set to the minimumTime property. Otherwise, this preserves the maximumTime 特性。

If the range is narrower then a time interval whose end it spans, for example a week that spans the end of a month, users can only edit the date to one in the later part of the range if keyboard-tracking is disabled.

另请参阅 minimumDate , maximumDate , setDateTimeRange (), QDate::isValid (),和 键盘追踪 .

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

Set the range of allowed date-times for the date time edit.

This convenience function sets the minimumDateTime and maximumDateTime 特性。

setDateTimeRange(min, max);
					

is analogous to:

setMinimumDateTime(min);
setMaximumDateTime(max);
					

If either min or max is invalid, this function does nothing. If max is less than min , min is used also as max .

If the range is narrower then a time interval whose end it spans, for example a week that spans the end of a month, users can only edit the date-time to one in the later part of the range if keyboard-tracking is disabled.

该函数在 Qt 4.4 引入。

另请参阅 minimumDateTime , maximumDateTime , setDateRange (), setTimeRange (), QDateTime::isValid (),和 键盘追踪 .

void QDateTimeEdit:: setSelectedSection ( QDateTimeEdit::Section section )

Selects section 。若 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.

该函数在 Qt 4.2 引入。

另请参阅 currentSection ().

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

Set the range of allowed times for the date time edit.

This convenience function sets the minimumTime and maximumTime 特性。

Note that these only constrain the date time edit's value on, respectively, the minimumDate and maximumDate . When these date properties do not coincide, times after max are allowed on dates before maximumDate and times before min are allowed on dates after minimumDate .

setTimeRange(min, max);
					

is analogous to:

setMinimumTime(min);
setMaximumTime(max);
					

If either min or max is invalid, this function does nothing. This function preserves the minimumDate and maximumDate properties. If those properties coincide and max is less than min , min is used as max .

If the range is narrower then a time interval whose end it spans, for example the interval from ten to an hour to ten past the same hour, users can only edit the time to one in the later part of the range if keyboard-tracking is disabled.

另请参阅 minimumTime , maximumTime , setDateTimeRange (), QTime::isValid (),和 键盘追踪 .

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

重实现: QAbstractSpinBox::sizeHint () const.

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

重实现: QAbstractSpinBox::stepBy (int steps).

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

重实现: QAbstractSpinBox::stepEnabled () const.

[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 () 和 validate ().

QTime QDateTimeEdit:: time () const

返回日期时间编辑的时间。

注意: Getter 函数对于特性 time。

另请参阅 setTime ().

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

重实现: QAbstractSpinBox::validate (QString &input, int &pos) const.

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

重实现: QAbstractSpinBox::wheelEvent (QWheelEvent *event).