QDateTimeEdit Class▲
-
Header: QDateTimeEdit
-
qmake: QT += widgets
-
Inherits: QAbstractSpinBox
-
Group: QDateTimeEdit is part of basicwidgets
Detailed Description▲

QDateTimeEdit allows the user to edit dates by using the keyboard or the arrow keys to increase and decrease date and time values. The arrow keys can be used to move from section to section within the QDateTimeEdit box. Dates and times appear in accordance with the format set; see setDisplayFormat().
QDateTimeEdit *
dateEdit =
new
QDateTimeEdit(QDate::
currentDate());
dateEdit-&
gt;setMinimumDate(QDate::
currentDate().addDays(-
365
));
dateEdit-&
gt;setMaximumDate(QDate::
currentDate().addDays(365
));
dateEdit-&
gt;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.
Using a Pop-up Calendar 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().
See Also▲
Member Type Documentation▲
enum QDateTimeEdit::Section▲
flags QDateTimeEdit::Sections
Constant |
|
---|---|
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.
Property Documentation▲
[since 4.2] 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.
This property was introduced in Qt 4.2.
Access functions:
-
bool calendarPopup() const
-
void setCalendarPopup(bool enable)
See Also▲
See also setDisplayFormat()
currentSection : Section▲
This property holds the current section of the spinbox.
Access functions:
-
currentSection() const
-
void setCurrentSection( section)
[since 4.3] 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.
This property was introduced in Qt 4.3.
Access functions:
-
int currentSectionIndex() const
-
void setCurrentSectionIndex(int index)
See Also▲
See also setCurrentSection(), 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.
Access functions:
-
date() const
-
void setDate(const &date)
Notifier signal:
-
void dateChanged(const &date)
See Also▲
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 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 property.
If the 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.
Access functions:
-
dateTime() const
-
void setDateTime(const &dateTime)
Notifier signal:
-
void dateTimeChanged(const &datetime)
See Also▲
See also 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() and QDateTime::fromString()
Example format strings (assuming that the date is 2nd of July 1969):
Format |
Result |
---|---|
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.
Access functions:
-
displayFormat() const
-
void setDisplayFormat(const &format)
See Also▲
See also QDateTime::toString(), displayedSections()
[read-only] 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.
Access functions:
-
displayedSections() const
See Also▲
See also 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 property.
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().
Access functions:
-
maximumDate() const
-
void setMaximumDate(const &max)
-
void clearMaximumDate()
See Also▲
See also minimumDate, maximumTime, maximumDateTime, setDateRange(), QDate::isValid()
[since 4.4] 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 property.
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().
This property was introduced in Qt 4.4.
Access functions:
-
maximumDateTime() const
-
void setMaximumDateTime(const &dt)
-
void clearMaximumDateTime()
See Also▲
See also 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 property.
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().
Access functions:
-
maximumTime() const
-
void setMaximumTime(const &max)
-
void clearMaximumTime()
See Also▲
See also 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 property.
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().
Access functions:
-
minimumDate() const
-
void setMinimumDate(const &min)
-
void clearMinimumDate()
See Also▲
See also maximumDate, minimumTime, minimumDateTime, setDateRange(), QDate::isValid()
[since 4.4] 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().
This property was introduced in Qt 4.4.
Access functions:
-
minimumDateTime() const
-
void setMinimumDateTime(const &dt)
-
void clearMinimumDateTime()
See Also▲
See also 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 property.
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().
Access functions:
-
minimumTime() const
-
void setMinimumTime(const &min)
-
void clearMinimumTime()
See Also▲
See also maximumTime, minimumDate, minimumDateTime, setTimeRange(), QTime::isValid()
[read-only, since 4.3] 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.
Access functions:
-
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.
Access functions:
-
time() const
-
void setTime(const &time)
Notifier signal:
-
void timeChanged(const &time)
See Also▲
[since 4.4] timeSpec : Qt::TimeSpec▲
This property holds the current timespec used by the date time edit.
This property was introduced in Qt 4.4.
Access functions:
-
timeSpec() const
-
void setTimeSpec( spec)
Member Function Documentation▲
[explicit] QDateTimeEdit::QDateTimeEdit(QWidget *parent = nullptr)▲
Constructs an empty date time editor with a parent.
[explicit] QDateTimeEdit::QDateTimeEdit(const QDateTime &datetime, QWidget *parent = nullptr)▲
Constructs an empty date time editor with a parent. The value is set to datetime.
[explicit] QDateTimeEdit::QDateTimeEdit(const QDate &date, QWidget *parent = nullptr)▲
Constructs an empty date time editor with a parent. The value is set to date.
[explicit] QDateTimeEdit::QDateTimeEdit(const QTime &time, QWidget *parent = nullptr)▲
Constructs an empty date time editor with a parent. The value is set to time.
[virtual] QDateTimeEdit::~QDateTimeEdit()▲
Destructor.
[since 4.4] 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.
See Also▲
See also setCalendarWidget()
[override virtual] void QDateTimeEdit::clear()▲
Reimplements: QAbstractSpinBox::clear().
QDate QDateTimeEdit::date() const▲
Returns the date of the date time edit.
Getter function for property date.
See Also▲
See also setDate()
void QDateTimeEdit::dateChanged(const QDate &date)▲
This signal is emitted whenever the date is changed. The new date is passed in date.
Notifier signal for property date.
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.
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.
See Also▲
See also textFromDateTime(), validate()
[override virtual] bool QDateTimeEdit::event(QEvent *event)▲
Reimplements: QAbstractSpinBox::event(QEvent *event).
[override virtual protected] void QDateTimeEdit::fixup(QString &input) const▲
Reimplements: QAbstractSpinBox::fixup(QString &input) const.
[override virtual protected] void QDateTimeEdit::focusInEvent(QFocusEvent *event)▲
Reimplements: QAbstractSpinBox::focusInEvent(QFocusEvent *event).
[override virtual protected] bool QDateTimeEdit::focusNextPrevChild(bool next)▲
Reimplements: QWidget::focusNextPrevChild(bool next).
[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.
See Also▲
See also QStyleOption::initFrom()
[override virtual protected] void QDateTimeEdit::keyPressEvent(QKeyEvent *event)▲
Reimplements: QAbstractSpinBox::keyPressEvent(QKeyEvent *event).
[override virtual protected] void QDateTimeEdit::mousePressEvent(QMouseEvent *event)▲
Reimplements: QAbstractSpinBox::mousePressEvent(QMouseEvent *event).
[override virtual protected] void QDateTimeEdit::paintEvent(QPaintEvent *event)▲
Reimplements: QAbstractSpinBox::paintEvent(QPaintEvent *event).
[since 4.3] 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,
This function was introduced in Qt 4.3.
QString QDateTimeEdit::sectionText(QDateTimeEdit::Section section) const▲
[since 4.4] 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.
calendarPopup must be set to true before setting the calendar widget.
This function was introduced in Qt 4.4.
See Also▲
See also 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 properties.
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 is equal to min and the maximumTime property was less then the minimumTime property, the maximumTime property is set to the minimumTime property. Otherwise, this preserves the maximumTime property.
See Also▲
See also minimumDate, maximumDate, setDateTimeRange(), QDate::isValid()
[since 4.4] 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 properties.
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.
This function was introduced in Qt 4.4.
See Also▲
See also minimumDateTime, maximumDateTime, setDateRange(), setTimeRange(), QDateTime::isValid()
[since 4.2] void QDateTimeEdit::setSelectedSection(QDateTimeEdit::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.
See Also▲
See also 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 properties.
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.
See Also▲
See also minimumTime, maximumTime, setDateTimeRange(), QTime::isValid()
[override virtual] QSize QDateTimeEdit::sizeHint() const▲
Reimplements: QAbstractSpinBox::sizeHint() const.
[override virtual] void QDateTimeEdit::stepBy(int steps)▲
Reimplements: QAbstractSpinBox::stepBy(int steps).
[override virtual protected] QAbstractSpinBox::StepEnabled QDateTimeEdit::stepEnabled() const▲
Reimplements: 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().
See Also▲
See also dateTimeFromText(), validate()
QTime QDateTimeEdit::time() const▲
Returns the time of the date time edit.
Getter function for property time.
See Also▲
See also setTime()
void QDateTimeEdit::timeChanged(const QTime &time)▲
This signal is emitted whenever the time is changed. The new time is passed in time.
Notifier signal for property time.
[override virtual protected] QValidator::State QDateTimeEdit::validate(QString &text, int &pos) const▲
Reimplements: QAbstractSpinBox::validate(QString &input, int &pos) const.
[override virtual protected] void QDateTimeEdit::wheelEvent(QWheelEvent *event)▲
Reimplements: QAbstractSpinBox::wheelEvent(QWheelEvent *event).