QDateTimeEdit Class

The QDateTimeEdit class provides a widget for editing dates and times. More...

Header: #include <QDateTimeEdit>
qmake: QT += widgets
Inherits: QAbstractSpinBox
Inherited By:

QDateEdit and QTimeEdit

Public Types

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

Properties

Public Functions

bool calendarPopup() 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
int sectionCount() const
void setCalendarPopup(bool enable)
void setCurrentSection(QDateTimeEdit::Section section)
void setCurrentSectionIndex(int index)
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 setTimeSpec(Qt::TimeSpec spec)
QTime time() const
Qt::TimeSpec timeSpec() const

Public Slots

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

Signals

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

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->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.

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

Keyboard Tracking

When keyboard tracking 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.

See also QDateEdit, QTimeEdit, QDate, and QTime.

Member Type Documentation

enum QDateTimeEdit::Section

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

Property Documentation

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

currentSection : Section

This property holds the current section of the spinbox.

Access functions:

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.

This property was introduced in Qt 4.3.

Access functions:

int currentSectionIndex() const
void setCurrentSectionIndex(int index)

See also setCurrentSection() and 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:

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

Notifier signal:

void dateChanged(const QDate &date)

See also 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 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:

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

Notifier signal:

void dateTimeChanged(const QDateTime &dateTime)

See also date, time, minimumDateTime, and 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):

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 21st (2000-2099).

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

Access functions:

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

See also 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.

Access functions:

QDateTimeEdit::Sections displayedSections() const

See also setDisplayFormat() and 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:

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

See also minimumDate, maximumTime, maximumDateTime, setDateRange(), QDate::isValid(), and Keyboard Tracking.

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:

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

See also minimumDateTime, maximumTime, maximumDate(), setDateTimeRange(), QDateTime::isValid(), and Keyboard Tracking.

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:

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

See also minimumTime, maximumDate, maximumDateTime, setTimeRange(), QTime::isValid(), and Keyboard Tracking.

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:

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

See also maximumDate, minimumTime, minimumDateTime, setDateRange(), QDate::isValid(), and Keyboard Tracking.

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:

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

See also maximumDateTime, minimumTime, minimumDate, setDateTimeRange(), QDateTime::isValid(), and Keyboard Tracking.

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:

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

See also maximumTime, minimumDate, minimumDateTime, setTimeRange(), QTime::isValid(), and Keyboard Tracking.

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:

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

Notifier signal:

void timeChanged(const QTime &time)

See also 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.

Access functions:

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