📄 qdatetimeedit.cpp
字号:
default: break; } return QAbstractSpinBox::event(event);}/*! \reimp*/void QDateTimeEdit::clear(){ Q_D(QDateTimeEdit); d->clearSection(d->currentSectionIndex);}/*! \reimp*/void QDateTimeEdit::keyPressEvent(QKeyEvent *event){ Q_D(QDateTimeEdit); int oldCurrent = d->currentSectionIndex; bool select = true; bool inserted = false; switch (event->key()) {#ifdef QT_KEYPAD_NAVIGATION case Qt::Key_NumberSign: //shortcut to popup calendar if (QApplication::keypadNavigationEnabled() && d->showCalendarPopup()) { d->initCalendarPopup(); d->positionCalendarPopup(); d->monthCalendar->show(); return; } break; case Qt::Key_Select: if (QApplication::keypadNavigationEnabled()) { if (hasEditFocus()) { if (d->focusOnButton) { d->initCalendarPopup(); d->positionCalendarPopup(); d->monthCalendar->show(); d->focusOnButton = false; return; } setEditFocus(false); selectAll(); } else { setEditFocus(true); //hide cursor d->edit->d_func()->setCursorVisible(false); if (d->edit->d_func()->cursorTimer > 0) killTimer(d->edit->d_func()->cursorTimer); d->edit->d_func()->cursorTimer = 0; d->setSelected(0); } } return;#endif case Qt::Key_Enter: case Qt::Key_Return: d->interpret(AlwaysEmit); d->setSelected(d->currentSectionIndex, true); event->ignore(); emit editingFinished(); return; default: if (!d->isSeparatorKey(event)) { inserted = select = !event->text().isEmpty() && event->text().at(0).isPrint() && !(event->modifiers() & ~Qt::ShiftModifier); break; } case Qt::Key_Left: case Qt::Key_Right: if (event->key() == Qt::Key_Left || event->key() == Qt::Key_Right) {#ifdef QT_KEYPAD_NAVIGATION if (!QApplication::keypadNavigationEnabled() || !hasEditFocus()) { select = false; break; }#endif if (!(event->modifiers() & Qt::ControlModifier)) { select = false; break; }#ifdef Q_WS_MAC else { select = (event->modifiers() & Qt::ShiftModifier); break; }#endif } // else fall through case Qt::Key_Backtab: case Qt::Key_Tab: { event->accept(); if (d->specialValue()) { d->edit->setSelection(d->edit->cursorPosition(), 0); return; } const bool forward = event->key() != Qt::Key_Left && event->key() != Qt::Key_Backtab && (event->key() != Qt::Key_Tab || !(event->modifiers() & Qt::ShiftModifier)); int newSection = d->nextPrevSection(d->currentSectionIndex, forward);#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled()) { if (d->focusOnButton) { newSection = forward ? 0 : d->sectionNodes.size() - 1; d->focusOnButton = false; update(); } else if (newSection < 0 && select && d->showCalendarPopup()) { setSelectedSection(NoSection); d->focusOnButton = true; update(); return; } } // only allow date/time sections to be selected. if (newSection & ~(QDateTimeParser::TimeSectionMask | QDateTimeParser::DateSectionMask)) return;#endif d->edit->deselect(); d->edit->setCursorPosition(d->sectionPos(newSection)); QDTEDEBUG << d->sectionPos(newSection); if (select) d->setSelected(newSection, true); return; } } QAbstractSpinBox::keyPressEvent(event); if (select && !(event->modifiers() & Qt::ShiftModifier) && !d->edit->hasSelectedText()) { if (inserted && d->sectionAt(d->edit->cursorPosition()) == QDateTimeParser::NoSectionIndex) { QString str = d->displayText(); int pos = d->edit->cursorPosition(); QValidator::State state; d->validateAndInterpret(str, pos, state); if (state == QValidator::Acceptable && (d->sectionNode(oldCurrent).count != 1 || d->sectionSize(oldCurrent) == d->sectionMaxSize(oldCurrent))) { QDTEDEBUG << "Setting currentsection to" << d->closestSection(d->edit->cursorPosition(), true) << event->key() << oldCurrent; const int tmp = d->closestSection(d->edit->cursorPosition(), true); if (tmp >= 0) d->currentSectionIndex = tmp; } } if (d->currentSectionIndex != oldCurrent) { d->setSelected(d->currentSectionIndex); } } if (d->specialValue()) { d->edit->setSelection(d->edit->cursorPosition(), 0); }}/*! \reimp*/#ifndef QT_NO_WHEELEVENTvoid QDateTimeEdit::wheelEvent(QWheelEvent *event){ QAbstractSpinBox::wheelEvent(event);}#endif/*! \reimp*/void QDateTimeEdit::focusInEvent(QFocusEvent *event){ Q_D(QDateTimeEdit); QAbstractSpinBox::focusInEvent(event); QString *frm = 0; const int oldPos = d->edit->cursorPosition(); if (!d->formatExplicitlySet) { if (d->displayFormat == d->defaultTimeFormat) { frm = &d->defaultTimeFormat; } else if (d->displayFormat == d->defaultDateFormat) { frm = &d->defaultDateFormat; } if (frm) { d->readLocaleSettings(); if (d->displayFormat != *frm) { setDisplayFormat(*frm); d->formatExplicitlySet = false; d->edit->setCursorPosition(oldPos); } } } const bool oldHasHadFocus = d->hasHadFocus; d->hasHadFocus = true; bool first = true; switch (event->reason()) { case Qt::BacktabFocusReason: first = false; break; case Qt::MouseFocusReason: case Qt::PopupFocusReason: return; case Qt::ActiveWindowFocusReason: if (oldHasHadFocus) return; case Qt::ShortcutFocusReason: case Qt::TabFocusReason: default: break; } if (isRightToLeft()) first = !first; d->updateEdit(); // needed to make it update specialValueText d->setSelected(first ? 0 : d->sectionNodes.size() - 1);}/*! \reimp*/bool QDateTimeEdit::focusNextPrevChild(bool next){ Q_D(QDateTimeEdit); if (!focusWidget()) return false; const int newSection = d->nextPrevSection(d->currentSectionIndex, next); switch (d->sectionType(newSection)) { case QDateTimeParser::NoSection: case QDateTimeParser::FirstSection: case QDateTimeParser::LastSection: break; default: return false; } return QAbstractSpinBox::focusNextPrevChild(next);}/*! \reimp*/void QDateTimeEdit::stepBy(int steps){ Q_D(QDateTimeEdit);#ifdef QT_KEYPAD_NAVIGATION // with keypad navigation and not editFocus, left right change the date/time by a fixed amount. if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { // if date based, shift by day. else shift by 15min if (d->sections & DateSections_Mask) setDateTime(dateTime().addDays(steps)); else { int minutes = time().hour()*60 + time().minute(); int blocks = minutes/15; blocks += steps; /* rounding involved */ if (minutes % 15) { if (steps < 0) { blocks += 1; // do one less step; } } minutes = blocks * 15; /* need to take wrapping into account */ if (!d->wrapping) { int max_minutes = d->maximum.toTime().hour()*60 + d->maximum.toTime().minute(); int min_minutes = d->minimum.toTime().hour()*60 + d->minimum.toTime().minute(); if (minutes >= max_minutes) { setTime(maximumTime()); return; } else if (minutes <= min_minutes) { setTime(minimumTime()); return; } } setTime(QTime(minutes/60, minutes%60)); } return; }#endif // don't optimize away steps == 0. This is the only way to select // the currentSection in Qt 4.1.x d->setValue(d->stepBy(d->currentSectionIndex, steps, false), EmitIfChanged); d->updateCache(d->value, d->displayText()); d->setSelected(d->currentSectionIndex);}/*! This virtual function is used by the date time edit whenever it needs to display \a dateTime. If you reimplement this, you may also need to reimplement valueFromText() and validate(). \sa dateTimeFromText(), validate()*/QString QDateTimeEdit::textFromDateTime(const QDateTime &dateTime) const{ Q_D(const QDateTimeEdit); return dateTime.toString(d->displayFormat);}/*! Returns an appropriate datetime for the given \a text. This virtual function is used by the datetime edit whenever it needs to interpret text entered by the user as a value. \sa textFromDateTime(), validate()*/QDateTime QDateTimeEdit::dateTimeFromText(const QString &text) const{ Q_D(const QDateTimeEdit); QString copy = text; int pos = d->edit->cursorPosition(); QValidator::State state = QValidator::Acceptable; return d->validateAndInterpret(copy, pos, state).toDateTime();}/*! \reimp*/QValidator::State QDateTimeEdit::validate(QString &text, int &pos) const{ Q_D(const QDateTimeEdit); QValidator::State state; d->validateAndInterpret(text, pos, state); return state;}/*! \reimp*/void QDateTimeEdit::fixup(QString &input) const{ Q_D(const QDateTimeEdit); QValidator::State state; int copy = d->edit->cursorPosition(); d->validateAndInterpret(input, copy, state, true);}/*! \reimp*/QDateTimeEdit::StepEnabled QDateTimeEdit::stepEnabled() const{ Q_D(const QDateTimeEdit); if (d->readOnly) return StepEnabled(0); if (d->specialValue()) { return (d->minimum == d->maximum ? StepEnabled(0) : StepEnabled(StepUpEnabled)); } QAbstractSpinBox::StepEnabled ret = 0;#ifdef QT_KEYPAD_NAVIGATION if (QApplication::keypadNavigationEnabled() && !hasEditFocus()) { if (d->wrapping) return StepEnabled(StepUpEnabled | StepDownEnabled); // 3 cases. date, time, datetime. each case look // at just the relavant component. QVariant max, min, val; if (d->sections & DateSections_Mask == 0) { // time only, no date max = d->maximum.toTime(); min = d->minimum.toTime(); val = d->value.toTime(); } else if (d->sections & TimeSections_Mask == 0) { // date only, no time max = d->maximum.toDate(); min = d->minimum.toDate(); val = d->value.toDate(); } else { // both max = d->maximum; min = d->minimum; val = d->value; } if (val != min) ret |= QAbstractSpinBox::StepDownEnabled; if (val != max) ret |= QAbstractSpinBox::StepUpEnabled; return ret; }#endif switch (d->sectionType(d->currentSectionIndex)) { case QDateTimeParser::NoSection: case QDateTimeParser::FirstSection: case QDateTimeParser::LastSection: return 0; default: break; } if (d->wrapping) return StepEnabled(StepDownEnabled|StepUpEnabled); QVariant v = d->stepBy(d->currentSectionIndex, 1, true); if (v != d->value) { ret |= QAbstractSpinBox::StepUpEnabled; } v = d->stepBy(d->currentSectionIndex, -1, true); if (v != d->value) { ret |= QAbstractSpinBox::StepDownEnabled; } return ret;}/*! \reimp*/void QDateTimeEdit::mousePressEvent(QMouseEvent *event){ Q_D(QDateTimeEdit); if (!d->showCalendarPopup()) { QAbstractSpinBox::mousePressEvent(event); return; } d->updateHoverControl(event->pos()); if (d->hoverControl == QStyle::SC_ComboBoxArrow) { d->updateArrow(QStyle::State_Sunken); d->initCalendarPopup(); d->positionCalendarPopup(); //Show the calendar d->monthCalendar->show(); event->accept(); } else { QAbstractSpinBox::mousePressEvent(event); }}/*! \class QTimeEdit \brief The QTimeEdit class provides a widget for editing times based on the QDateTimeEdit widget.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -