📄 qdatetimeedit.cpp
字号:
\ingroup basicwidgets \mainclass Many of the properties and functions provided by QTimeEdit are implemented in QDateTimeEdit. The following properties are most relevant to users of this class: \list \o \l{QDateTimeEdit::time}{time} holds the date displayed by the widget. \o \l{QDateTimeEdit::minimumTime}{minimumTime} defines the minimum (earliest) time that can be set by the user. \o \l{QDateTimeEdit::maximumTime}{maximumTime} defines the maximum (latest) time that can be set by the user. \o \l{QDateTimeEdit::displayFormat}{displayFormat} contains a string that is used to format the time displayed in the widget. \endlist \table 100% \row \o \inlineimage windowsxp-timeedit.png Screenshot of a Windows XP style time editing widget \o A time editing widget shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}. \row \o \inlineimage macintosh-timeedit.png Screenshot of a Macintosh style time editing widget \o A time editing widget shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. \row \o \inlineimage plastique-timeedit.png Screenshot of a Plastique style time editing widget \o A time editing widget shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}. \endtable \sa QDateEdit, QDateTimeEdit*//*! Constructs an empty time editor with a \a parent.*/QTimeEdit::QTimeEdit(QWidget *parent) : QDateTimeEdit(QDATETIMEEDIT_TIME_MIN, parent){}/*! Constructs an empty time editor with a \a parent. The time is set to \a time.*/QTimeEdit::QTimeEdit(const QTime &time, QWidget *parent) : QDateTimeEdit(time, parent){}/*! \class QDateEdit \brief The QDateEdit class provides a widget for editing dates based on the QDateTimeEdit widget. \ingroup basicwidgets \mainclass Many of the properties and functions provided by QDateEdit are implemented in QDateTimeEdit. The following properties are most relevant to users of this class: \list \o \l{QDateTimeEdit::date}{date} holds the date displayed by the widget. \o \l{QDateTimeEdit::minimumDate}{minimumDate} defines the minimum (earliest) date that can be set by the user. \o \l{QDateTimeEdit::maximumDate}{maximumDate} defines the maximum (latest) date that can be set by the user. \o \l{QDateTimeEdit::displayFormat}{displayFormat} contains a string that is used to format the date displayed in the widget. \endlist \table 100% \row \o \inlineimage windowsxp-dateedit.png Screenshot of a Windows XP style date editing widget \o A date editing widget shown in the \l{Windows XP Style Widget Gallery}{Windows XP widget style}. \row \o \inlineimage macintosh-dateedit.png Screenshot of a Macintosh style date editing widget \o A date editing widget shown in the \l{Macintosh Style Widget Gallery}{Macintosh widget style}. \row \o \inlineimage plastique-dateedit.png Screenshot of a Plastique style date editing widget \o A date editing widget shown in the \l{Plastique Style Widget Gallery}{Plastique widget style}. \endtable \sa QTimeEdit, QDateTimeEdit*//*! Constructs an empty date editor with a \a parent.*/QDateEdit::QDateEdit(QWidget *parent) : QDateTimeEdit(QDATETIMEEDIT_DATE_INITIAL, parent){}/*! Constructs an empty date editor with a \a parent. The date is set to \a date.*/QDateEdit::QDateEdit(const QDate &date, QWidget *parent) : QDateTimeEdit(date, parent){}// --- QDateTimeEditPrivate ---/*! \internal Constructs a QDateTimeEditPrivate object*/QDateTimeEditPrivate::QDateTimeEditPrivate() : QDateTimeParser(QVariant::DateTime){ hasHadFocus = false; formatExplicitlySet = false; cacheGuard = false; fixday = true; allowEmpty = false; type = QVariant::DateTime; sections = 0; cachedDay = -1; currentSectionIndex = FirstSectionIndex; layoutDirection = QApplication::layoutDirection(); first.type = FirstSection; last.type = LastSection; none.type = NoSection; first.pos = 0; last.pos = -1; none.pos = -1; sections = 0; calendarPopup = false; minimum = QVariant(QDATETIMEEDIT_COMPAT_DATETIME_MIN); maximum = QVariant(QDATETIMEEDIT_DATETIME_MAX); arrowState = QStyle::State_None; monthCalendar = 0; readLocaleSettings();#ifdef QT_KEYPAD_NAVIGATION focusOnButton = false;#endif}void QDateTimeEditPrivate::updateEdit(){ const QString newText = (specialValue() ? specialValueText : textFromValue(value)); if (newText == displayText()) return; int selsize = edit->selectedText().size(); const bool sb = edit->blockSignals(true); edit->setText(newText); if (!specialValue()#ifdef QT_KEYPAD_NAVIGATION && !(QApplication::keypadNavigationEnabled() && !edit->hasEditFocus())#endif ) { int cursor = sectionPos(currentSectionIndex); QDTEDEBUG << "cursor is " << cursor << currentSectionIndex; cursor = qBound(0, cursor, displayText().size()); QDTEDEBUG << cursor; if (selsize > 0) { edit->setSelection(cursor, selsize); QDTEDEBUG << cursor << selsize; } else { edit->setCursorPosition(cursor); QDTEDEBUG << cursor; } } edit->blockSignals(sb);}/*! \internal Selects the section \a s. If \a forward is false selects backwards.*/void QDateTimeEditPrivate::setSelected(int sectionIndex, bool forward){ if ( specialValue()#ifdef QT_KEYPAD_NAVIGATION || (QApplication::keypadNavigationEnabled() && !edit->hasEditFocus())#endif ) { edit->selectAll(); } else { const SectionNode &node = sectionNode(sectionIndex); if (node.type == NoSection || node.type == LastSection || node.type == FirstSection) return; updateCache(value, displayText()); const int size = sectionSize(sectionIndex); if (forward) { edit->setSelection(sectionPos(node), size); } else { edit->setSelection(sectionPos(node) + size, -size); } }}/*! \internal Returns the section at index \a index or NoSection if there are no sections there.*/int QDateTimeEditPrivate::sectionAt(int pos) const{ if (pos < separators.first().size()) { return (pos == 0 ? FirstSectionIndex : NoSectionIndex); } else if (displayText().size() - pos < separators.last().size() + 1) { if (separators.last().size() == 0) { return sectionNodes.count() - 1; } return (pos == displayText().size() ? LastSectionIndex : NoSectionIndex); } updateCache(value, displayText()); for (int i=0; i<sectionNodes.size(); ++i) { const int tmp = sectionPos(i); if (pos < tmp + sectionSize(i)) { return (pos < tmp ? -1 : i); } } return -1;}/*! \internal Returns the closest section of index \a index. Searches forward for a section if \a forward is true. Otherwise searches backwards.*/int QDateTimeEditPrivate::closestSection(int pos, bool forward) const{ Q_ASSERT(pos >= 0); if (pos < separators.first().size()) { return forward ? 0 : FirstSectionIndex; } else if (displayText().size() - pos < separators.last().size() + 1) { return forward ? LastSectionIndex : sectionNodes.size() - 1; } updateCache(value, displayText()); for (int i=0; i<sectionNodes.size(); ++i) { const int tmp = sectionPos(sectionNodes.at(i)); if (pos < tmp + sectionSize(i)) { if (pos < tmp && !forward) { return i-1; } return i; } else if (i == sectionNodes.size() - 1 && pos > tmp) { return i; } } qWarning("QDateTimeEdit: Internal Error: closestSection returned NoSection"); return NoSectionIndex;}/*! \internal Returns a copy of the section that is before or after \a current, depending on \a forward.*/int QDateTimeEditPrivate::nextPrevSection(int current, bool forward) const{ Q_Q(const QDateTimeEdit); if (q->isRightToLeft()) forward = !forward; switch (current) { case FirstSectionIndex: return forward ? 0 : FirstSectionIndex; case LastSectionIndex: return (forward ? LastSectionIndex : sectionNodes.size() - 1); case NoSectionIndex: return FirstSectionIndex; default: break; } Q_ASSERT(current >= 0 && current < sectionNodes.size()); current += (forward ? 1 : -1); if (current >= sectionNodes.size()) { return LastSectionIndex; } else if (current < 0) { return FirstSectionIndex; } return current;}/*! \internal Clears the text of section \a s.*/void QDateTimeEditPrivate::clearSection(int index){ const QLatin1Char space(' '); int cursorPos = edit->cursorPosition(); bool blocked = edit->blockSignals(true); QString t = edit->text(); const int pos = sectionPos(index); if (pos == -1) { qWarning("QDateTimeEdit: Internal error (%s:%d)", __FILE__, __LINE__); return; } const int size = sectionSize(index); t.replace(pos, size, QString().fill(space, size)); edit->setText(t); edit->setCursorPosition(cursorPos); QDTEDEBUG << cursorPos; edit->blockSignals(blocked);}/*! \internal updates the cached values*/void QDateTimeEditPrivate::updateCache(const QVariant &val, const QString &str) const{ if (val != cachedValue || str != cachedText || cacheGuard) { cacheGuard = true; QString copy = str; int unused = edit->cursorPosition(); QValidator::State unusedState; validateAndInterpret(copy, unused, unusedState); cacheGuard = false; }}/*! \internal parses and validates \a input*/QVariant QDateTimeEditPrivate::validateAndInterpret(QString &input, int &/*position*/, QValidator::State &state, bool fixup) const{ if (input.isEmpty()) { if (sectionNodes.size() == 1) { state = QValidator::Intermediate; } else { state = QValidator::Invalid; } return getZeroVariant(); } else if (cachedText == input && !fixup) { state = cachedState; return cachedValue; } if (!specialValueText.isEmpty() && input == specialValueText) { state = QValidator::Acceptable; return minimum; } StateNode tmp = parse(input, value, fixup); input = tmp.input; state = QValidator::State(int(tmp.state)); if (state == QValidator::Acceptable) { if (tmp.conflicts && conflictGuard != tmp.value) { conflictGuard = tmp.value; clearCache(); input = textFromValue(tmp.value); updateCache(tmp.value, input); conflictGuard.clear(); } else { cachedText = input; cachedState = state; cachedValue = tmp.value; } } else { clearCache(); } return (tmp.value.isNull() ? getZeroVariant() : tmp.value);}/*! \internal \reimp*/QString QDateTimeEditPrivate::textFromValue(const QVariant &f) const{ Q_Q(const QDateTimeEdit); return q->textFromDateTime(f.toDateTime());}/*! \internal \reimp*/QVariant QDateTimeEditPrivate::valueFromText(const QString &f) const{ Q_Q(const QDateTimeEdit); return QVariant(q->dateTimeFromText(f));}/*! \internal Internal function called by QDateTimeEdit::stepBy(). Also takes a Section for which section to step on and a bool \a test for whether or not to modify the internal cachedDay variable. This is necessary because the function is called from the const function QDateTimeEdit::stepEnabled() as well as QDateTimeEdit::stepBy().*/QVariant QDateTimeEditPrivate::stepBy(int sectionIndex, int steps, bool test) const{ Q_Q(const QDateTimeEdit); QVariant v = value; QString str = displayText(); int pos = edit->cursorPosition(); const SectionNode sn = sectionNode(sectionIndex); int val; // to make sure it behaves reasonably when typing something and then stepping in non-tracking mode if (!test && pendingEmit) { if (q->validate(str, pos) != QValidator::Acceptable) { v = value; } else { v = valueFromText(str); } val = getDigit(v, sectionIndex); } else { val = getDigit(value, sectionIndex); } val += steps; const int min = absoluteMin(sectionIndex); const int max = absoluteMax(sectionIndex, value.toDateTime()); if (val < min) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -