QDateTimeEdit Class
The QDateTimeEdit class lets the user edit a date and a time. More…
| Header | #include <QDateTimeEdit> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Widgets) |
| Module | Nt Widgets C++ Classes |
| Inherits | QAbstractSpinBox |
| Inherited by | QDateEdit, QTimeEdit |
List of all members, including inherited members
Public Types
Public Functions
Reimplemented Public Functions
Public Slots
Signals
| void | dateTimeChanged(const QDateTime &datetime) |
| void | timeChanged(QTime time) |
| void | dateChanged(QDate date) |
Protected Functions
| virtual QDateTime | dateTimeFromText(const QString &text) const |
| virtual QString | textFromDateTime(const QDateTime &dt) const |
Reimplemented Protected Functions
| StepEnabled | stepEnabled() const |
| void | mousePressEvent(QMouseEvent *event) |
| void | keyPressEvent(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>.
| Constant | Description |
|---|---|
QDateTimeEdit::NoSection | No section. |
QDateTimeEdit::AmPmSection | AM or PM. |
QDateTimeEdit::MSecSection | The milliseconds. |
QDateTimeEdit::SecondSection | The seconds. |
QDateTimeEdit::MinuteSection | The minutes. |
QDateTimeEdit::HourSection | The hours. |
QDateTimeEdit::DaySection | The day. |
QDateTimeEdit::MonthSection | The month. |
QDateTimeEdit::YearSection | The year. |
QDateTimeEdit::TimeSections_Mask | The sections of the time. |
QDateTimeEdit::DateSections_Mask | The 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
QDateTimeEdit::QDateTimeEdit(QWidget *parent = nullptr)
Constructs a date time edit with the given parent, at 2000-01-01 00:00.
QDateTimeEdit::QDateTimeEdit(const QDateTime &datetime, QWidget *parent = nullptr)
Constructs a date time edit with the given datetime and parent.
QDateTimeEdit::QDateTimeEdit(QDate date, QWidget *parent = nullptr)
Constructs a date time edit that shows the date, with the given parent.
QDateTimeEdit::QDateTimeEdit(QTime time, QWidget *parent = nullptr)
Constructs a date time edit that shows the time, with the given parent.
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().
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().
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().
void QDateTimeEdit::dateTimeChanged(const QDateTime &datetime)
This signal is emitted when the date or the time changes to datetime.
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().
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().
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.
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.
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.
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.
void QDateTimeEdit::stepBy(int steps)
Reimplements QAbstractSpinBox::stepBy().
Steps the section that the cursor is in by steps, and selects it.
StepEnabled QDateTimeEdit::stepEnabled() const
Reimplements QAbstractSpinBox::stepEnabled().
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().
void QDateTimeEdit::timeChanged(QTime time)
This signal is emitted when the time changes to time.