6.0.0-rc4Source

QDateTimeEdit Class

The QDateTimeEdit class lets the user edit a date and a time. More…

Header#include <QDateTimeEdit>
CMaketarget_link_libraries(app PRIVATE Nt::Widgets)
ModuleNt Widgets C++ Classes
InheritsQAbstractSpinBox
Inherited byQDateEdit, QTimeEdit

List of all members, including inherited members

Public Types

enumSection
typedefSections

Public Functions

QDateTimeEdit(QWidget *parent = nullptr)
QDateTimeEdit(const QDateTime &datetime, QWidget *parent = nullptr)
QDateTimeEdit(QDate date, QWidget *parent = nullptr)
QDateTimeEdit(QTime time, QWidget *parent = nullptr)
~QDateTimeEdit()
QDateTimedateTime() const
QDatedate() const
QTimetime() const
QDateTimeminimumDateTime() const
voidclearMinimumDateTime()
voidsetMinimumDateTime(const QDateTime &dt)
QDateTimemaximumDateTime() const
voidclearMaximumDateTime()
voidsetMaximumDateTime(const QDateTime &dt)
voidsetDateTimeRange(const QDateTime &min, const QDateTime &max)
QDateminimumDate() const
voidsetMinimumDate(QDate min)
voidclearMinimumDate()
QDatemaximumDate() const
voidsetMaximumDate(QDate max)
voidclearMaximumDate()
voidsetDateRange(QDate min, QDate max)
QTimeminimumTime() const
voidsetMinimumTime(QTime min)
voidclearMinimumTime()
QTimemaximumTime() const
voidsetMaximumTime(QTime max)
voidclearMaximumTime()
voidsetTimeRange(QTime min, QTime max)
SectionsdisplayedSections() const
SectioncurrentSection() const
SectionsectionAt(int index) const
voidsetCurrentSection(Section section)
intcurrentSectionIndex() const
voidsetCurrentSectionIndex(int index)
intsectionCount() const
voidsetSelectedSection(Section section)
QStringsectionText(Section section) const
QStringdisplayFormat() const
voidsetDisplayFormat(const QString &format)
boolcalendarPopup() const
voidsetCalendarPopup(bool enable)
QCalendarWidget *calendarWidget() const
voidsetCalendarWidget(QCalendarWidget *calendarWidget)

Reimplemented Public Functions

voidclear()
voidstepBy(int steps)

Public Slots

voidsetDateTime(const QDateTime &dateTime)
voidsetDate(QDate date)
voidsetTime(QTime time)

Signals

voiddateTimeChanged(const QDateTime &datetime)
voidtimeChanged(QTime time)
voiddateChanged(QDate date)

Protected Functions

virtual QDateTimedateTimeFromText(const QString &text) const
virtual QStringtextFromDateTime(const QDateTime &dt) const

Reimplemented Protected Functions

StepEnabledstepEnabled() const
voidmousePressEvent(QMouseEvent *event)
voidkeyPressEvent(QKeyEvent *event)

Detailed Description

A date time edit shows a date and a time in the displayFormat(), divided into sections such as the day, the month and the hour. The arrow keys, the mouse wheel and the buttons step the section that the cursor is in; typing changes the text, which is taken when it is a valid date and time:

auto *start = new QDateTimeEdit(QDateTime::currentDateTime(), window);
start->setDisplayFormat("yyyy-MM-dd HH:mm");
start->setMinimumDateTime(QDateTime::currentDateTime());
connect(start, &QDateTimeEdit::dateTimeChanged, [](const QDateTime &dt) { schedule(dt); });

The format has the letters of QDate::toString() and QTime::toString(): d, dd, ddd, dddd, M, MM, MMM, MMMM, yy, yyyy, h, hh, H, HH, m, mm, s, ss, z, zzz, and AP or ap; text in single quotes is shown as it is. A step that goes past the end of a section stops there, or goes round to the other end of the section if the edit wraps; the result is kept in the range.

With setCalendarPopup(), the buttons open a QCalendarWidget to pick the date from.

QDateEdit and QTimeEdit edit only a date or a time.

See also QDateEdit, QTimeEdit and QCalendarWidget.

Differences from Qt

There is no QLocale, so the default formats are "yyyy-MM-dd HH:mm", "yyyy-MM-dd" and "HH:mm", the names are English, and yy is read as a year of the century of the current date. The time is always local time: there is no timeSpec() or setTimeZone(). Tab does not move between the sections, and the calendar popup opens from the step buttons, which are drawn as for any spin box. There is no setCalendar().

Member Type Documentation

enum QDateTimeEdit::Section

The sections of the text. Sections is a typedef for QFlags<Section>.

ConstantDescription
QDateTimeEdit::NoSectionNo section.
QDateTimeEdit::AmPmSectionAM or PM.
QDateTimeEdit::MSecSectionThe milliseconds.
QDateTimeEdit::SecondSectionThe seconds.
QDateTimeEdit::MinuteSectionThe minutes.
QDateTimeEdit::HourSectionThe hours.
QDateTimeEdit::DaySectionThe day.
QDateTimeEdit::MonthSectionThe month.
QDateTimeEdit::YearSectionThe year.
QDateTimeEdit::TimeSections_MaskThe sections of the time.
QDateTimeEdit::DateSections_MaskThe sections of the date.

typedef QDateTimeEdit::Sections

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

Member Function Documentation

[explicit] QDateTimeEdit::QDateTimeEdit(QWidget *parent = nullptr)

Constructs a date time edit with the given parent, at 2000-01-01 00:00.

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

Constructs a date time edit with the given datetime and parent.

[explicit] QDateTimeEdit::QDateTimeEdit(QDate date, QWidget *parent = nullptr)

Constructs a date time edit that shows the date, with the given parent.

[explicit] QDateTimeEdit::QDateTimeEdit(QTime time, QWidget *parent = nullptr)

Constructs a date time edit that shows the time, with the given parent.

[override] QDateTimeEdit::~QDateTimeEdit()

Destroys the edit.

bool QDateTimeEdit::calendarPopup() const

Returns whether the buttons open a calendar. The default is false.

QCalendarWidget * QDateTimeEdit::calendarWidget() const

Returns the calendar of the popup, creating it if there is none yet.

See also setCalendarWidget().

[override] void QDateTimeEdit::clear()

Reimplements QAbstractSpinBox::clear().

Clears the text; the value does not change until the user types a new one.

void QDateTimeEdit::clearMaximumDate()

Sets the latest date back to 9999-12-31.

void QDateTimeEdit::clearMaximumDateTime()

Sets the latest date and time back to 9999-12-31 23:59:59.999.

void QDateTimeEdit::clearMaximumTime()

Sets the time of the latest date and time back to 23:59:59.999.

void QDateTimeEdit::clearMinimumDate()

Sets the earliest date back to 1752-09-14.

void QDateTimeEdit::clearMinimumDateTime()

Sets the earliest date and time back to 1752-09-14 00:00.

void QDateTimeEdit::clearMinimumTime()

Sets the time of the earliest date and time back to 00:00.

Section QDateTimeEdit::currentSection() const

Returns the section that the cursor is in, which the keys and the buttons step.

See also setCurrentSection().

int QDateTimeEdit::currentSectionIndex() const

Returns the index of the section that the cursor is in.

QDate QDateTimeEdit::date() const

Returns the date.

See also setDate().

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

This signal is emitted when the date changes to date.

QDateTime QDateTimeEdit::dateTime() const

Returns the date and the time.

See also setDateTime().

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

This signal is emitted when the date or the time changes to datetime.

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

Returns the date and time that text shows in the display format, or an invalid QDateTime. Sections that the format does not have keep the value of the edit.

See also textFromDateTime().

Sections QDateTimeEdit::displayedSections() const

Returns the sections that the display format shows.

QString QDateTimeEdit::displayFormat() const

Returns the format of the text.

See also setDisplayFormat().

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

Reimplements QAbstractSpinBox::keyPressEvent().

Up, Down, Page Up and Page Down step the section that the cursor is in and keep it selected. With calendarPopup(), F4 and Alt+Down open the calendar.

QDate QDateTimeEdit::maximumDate() const

Returns the date of maximumDateTime().

QDateTime QDateTimeEdit::maximumDateTime() const

Returns the latest date and time, 9999-12-31 23:59:59.999 by default.

QTime QDateTimeEdit::maximumTime() const

Returns the time of maximumDateTime().

QDate QDateTimeEdit::minimumDate() const

Returns the date of minimumDateTime().

QDateTime QDateTimeEdit::minimumDateTime() const

Returns the earliest date and time, 1752-09-14 00:00 by default.

QTime QDateTimeEdit::minimumTime() const

Returns the time of minimumDateTime().

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

Reimplements QAbstractSpinBox::mousePressEvent().

With calendarPopup(), the buttons open the calendar.

Section QDateTimeEdit::sectionAt(int index) const

Returns the section at index in the text, or NoSection.

int QDateTimeEdit::sectionCount() const

Returns the number of sections of the text.

QString QDateTimeEdit::sectionText(Section section) const

Returns the text of the first section, or an empty string if the format does not have it.

void QDateTimeEdit::setCalendarPopup(bool enable)

Sets whether the buttons open a calendar to pick the date from to enable.

See also calendarWidget().

void QDateTimeEdit::setCalendarWidget(QCalendarWidget *calendarWidget)

Makes calendarWidget the calendar of the popup. The edit takes it and deletes the one it had.

void QDateTimeEdit::setCurrentSection(Section section)

Moves the cursor to the first section of the text, if the format has it.

void QDateTimeEdit::setCurrentSectionIndex(int index)

Moves the cursor to the section at index.

[slot] void QDateTimeEdit::setDate(QDate date)

Sets the date to date, keeping the time.

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

Sets the range of dates to min to max, from the start of the first day to the end of the last.

[slot] void QDateTimeEdit::setDateTime(const QDateTime &dateTime)

Sets the date and the time to dateTime, brought into the range. Emits dateTimeChanged(), and dateChanged() or timeChanged() for the part that changes.

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

Sets the range to min to max, and brings the value into it.

void QDateTimeEdit::setDisplayFormat(const QString &format)

Sets the format of the text to format, with the letters of QDate::toString() and QTime::toString().

void QDateTimeEdit::setMaximumDate(QDate max)

Sets the latest date to max, at the end of the day.

void QDateTimeEdit::setMaximumDateTime(const QDateTime &dt)

Sets the latest date and time to dt. The earliest falls to it if it was later.

void QDateTimeEdit::setMaximumTime(QTime max)

Sets the time of the latest date and time to max.

void QDateTimeEdit::setMinimumDate(QDate min)

Sets the earliest date to min, at the start of the day.

void QDateTimeEdit::setMinimumDateTime(const QDateTime &dt)

Sets the earliest date and time to dt. The latest grows to it if it was earlier.

void QDateTimeEdit::setMinimumTime(QTime min)

Sets the time of the earliest date and time to min.

void QDateTimeEdit::setSelectedSection(Section section)

Selects the text of the first section, and moves the cursor there.

[slot] void QDateTimeEdit::setTime(QTime time)

Sets the time to time, keeping the date.

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

Sets the range of times to min to max, on the dates of the range.

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

Reimplements QAbstractSpinBox::stepBy().

Steps the section that the cursor is in by steps, and selects it.

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

Reimplements QAbstractSpinBox::stepEnabled().

[virtual, protected] QString QDateTimeEdit::textFromDateTime(const QDateTime &dt) const

Returns the text that shows dt in the display format.

See also dateTimeFromText().

QTime QDateTimeEdit::time() const

Returns the time.

See also setTime().

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

This signal is emitted when the time changes to time.