📄 qdatetimeedit.cpp
字号:
void QDateTimeEdit::setMaximumTime(const QTime &max){ Q_D(QDateTimeEdit); if (max.isValid()) { const QVariant m(QDateTime(d->maximum.toDate(), max)); d->setRange((d->variantCompare(d->minimum, m) < 0 ? d->minimum : m), m); }}void QDateTimeEdit::clearMaximumTime(){ setMaximumTime(QTIME_MAX);}/*! Convenience function to set minimum and maximum date with one function call. \code setDateRange(min, max); \endcode is analogous to: \code setMinimumDate(min); setMaximumDate(max); \endcode If either \a min or \a max are not valid, this function does nothing. \sa setMinimumDate(), maximumDate(), setMaximumDate(), clearMinimumDate(), setMinimumTime(), maximumTime(), setMaximumTime(), clearMinimumTime(), QDate::isValid()*/void QDateTimeEdit::setDateRange(const QDate &min, const QDate &max){ Q_D(QDateTimeEdit); if (min.isValid() && max.isValid()) { d->setRange(QVariant(QDateTime(min, d->minimum.toTime())), QVariant(QDateTime(max, d->maximum.toTime()))); }}/*! Convenience function to set minimum and maximum time with one function call. \code setTimeRange(min, max); \endcode is analogous to: \code setMinimumTime(min); setMaximumTime(max); \endcode If either \a min or \a max are not valid, this function does nothing. \sa setMinimumDate(), maximumDate(), setMaximumDate(), clearMinimumDate(), setMinimumTime(), maximumTime(), setMaximumTime(), clearMinimumTime(), QTime::isValid()*/void QDateTimeEdit::setTimeRange(const QTime &min, const QTime &max){ Q_D(QDateTimeEdit); if (min.isValid() && max.isValid()) { d->setRange(QVariant(QDateTime(d->minimum.toDate(), min)), QVariant(QDateTime(d->maximum.toDate(), max))); }}/*! \property QDateTimeEdit::displayedSections \brief the currently displayed fields of the date time edit Returns a bit set of the displayed sections for this format. \a setDisplayFormat(), displayFormat()*/QDateTimeEdit::Sections QDateTimeEdit::displayedSections() const{ Q_D(const QDateTimeEdit); return d->sections;}/*! \property QDateTimeEdit::currentSection \brief the current section of the spinbox \a setCurrentSection()*/QDateTimeEdit::Section QDateTimeEdit::currentSection() const{ Q_D(const QDateTimeEdit); return d->convertToPublic(d->sectionType(d->currentSectionIndex));}void QDateTimeEdit::setCurrentSection(Section section){ Q_D(QDateTimeEdit); if (section == NoSection || !(section & d->sections)) return; d->updateCache(d->value, d->displayText()); const int size = d->sectionNodes.size(); int index = d->currentSectionIndex + 1; for (int i=0; i<2; ++i) { while (index < size) { if (d->convertToPublic(d->sectionType(index)) == section) { d->edit->setCursorPosition(d->sectionPos(index)); QDTEDEBUG << d->sectionPos(index); return; } ++index; } index = 0; }}/*! \fn QString QDateTimeEdit::sectionText(Section section) const Returns the text from the given \a section. ### note about not working when not Acceptable \sa currentSection()*/QString QDateTimeEdit::sectionText(Section section) const{ Q_D(const QDateTimeEdit); if (section == QDateTimeEdit::NoSection || !(section & d->sections)) { return QString(); } d->updateCache(d->value, d->displayText()); const int sectionIndex = d->absoluteIndex(section, 0); if (sectionIndex < 0) return QString(); return d->sectionText(d->displayText(), sectionIndex, d->sectionPos(sectionIndex));}/*! \property QDateTimeEdit::displayFormat \brief the format used to display the time/date of the date time edit This format is the same as the one used described in QDateTime::toString() and QDateTime::fromString() Example format strings(assuming that the date is 2nd of July 1969): \table \header \i Format \i Result \row \i dd.MM.yyyy \i 02.07.1969 \row \i MMM d yy \i Jul 2 69 \row \i MMMM d yy \i July 2 69 \endtable If you specify an invalid format the format will not be set. \sa QDateTime::toString(), displayedSections()*/QString QDateTimeEdit::displayFormat() const{ Q_D(const QDateTimeEdit); return d->displayFormat;}void QDateTimeEdit::setDisplayFormat(const QString &format){ Q_D(QDateTimeEdit); if (d->parseFormat(format)) { d->sections = d->convertSections(d->display); d->clearCache(); d->currentSectionIndex = qMin(d->currentSectionIndex, d->sectionNodes.size() - 1); const bool timeShown = (d->sections & TimeSections_Mask); const bool dateShown = (d->sections & DateSections_Mask); Q_ASSERT(dateShown || timeShown); if (timeShown && !dateShown) { setDateRange(d->value.toDate(), d->value.toDate()); } else if (dateShown && !timeShown) { setTimeRange(QTIME_MIN, QTIME_MAX); d->value = QVariant(QDateTime(d->value.toDate(), QTime())); } d->updateEdit(); d->edit->setCursorPosition(0); QDTEDEBUG << 0; d->_q_editorCursorPositionChanged(-1, 0); }}/*! \reimp*/QSize QDateTimeEdit::sizeHint() const{ Q_D(const QAbstractSpinBox); ensurePolished(); const QFontMetrics fm(fontMetrics()); int h = d->edit->sizeHint().height(); int w = 0; QString s; s = d->textFromValue(d->minimum) + QLatin1String(" "); w = qMax<int>(w, fm.width(s)); s = d->textFromValue(d->maximum) + QLatin1String(" "); w = qMax<int>(w, fm.width(s)); if (d->specialValueText.size()) { s = d->specialValueText; w = qMax<int>(w, fm.width(s)); } w += 2; // cursor blinking space QStyleOptionSpinBox opt = d->getStyleOption(); QSize hint(w, h); QSize extra(35, 6); opt.rect.setSize(hint + extra); extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxEditField, this).size(); // get closer to final result by repeating the calculation opt.rect.setSize(hint + extra); extra += hint - style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxEditField, this).size(); hint += extra; opt.rect = rect(); return style()->sizeFromContents(QStyle::CT_SpinBox, &opt, hint, this) .expandedTo(QApplication::globalStrut());}/*! \reimp*/bool QDateTimeEdit::event(QEvent *e){ Q_D(QDateTimeEdit); switch (e->type()) { case QEvent::ApplicationLayoutDirectionChange: setDisplayFormat(d->displayFormat); break; default: break; } return QAbstractSpinBox::event(e);}/*! \reimp*/void QDateTimeEdit::clear(){ Q_D(QDateTimeEdit); d->clearSection(d->currentSectionIndex);}/*! \reimp*/void QDateTimeEdit::keyPressEvent(QKeyEvent *e){ Q_D(QDateTimeEdit); int oldCurrent = d->currentSectionIndex; bool select = true; bool inserted = false; bool forward = true; switch (e->key()) { case Qt::Key_Enter: case Qt::Key_Return: d->interpret(AlwaysEmit); d->setSelected(d->currentSectionIndex, true); e->ignore(); emit editingFinished(); return; case Qt::Key_Left: forward = false; case Qt::Key_Right: if (!(e->modifiers() & Qt::ControlModifier)) { select = false; break; }#ifdef Q_WS_MAC else { select = (e->modifiers() & Qt::ShiftModifier); break; }#endif // fallthroughs intended case Qt::Key_Backtab: case Qt::Key_Tab: { e->accept(); if (d->specialValue()) { d->edit->setSelection(d->edit->cursorPosition(), 0); return; } if (e->key() == Qt::Key_Backtab || (e->key() == Qt::Key_Tab && e->modifiers() & Qt::ShiftModifier)) { forward = false; } const int newSection = d->nextPrevSection(d->currentSectionIndex, forward); d->edit->deselect(); d->edit->setCursorPosition(d->sectionPos(newSection)); QDTEDEBUG << d->sectionPos(newSection); if (select) d->setSelected(newSection, true); return; } default: inserted = select = !e->text().isEmpty() && e->text().at(0).isPrint() && !(e->modifiers() & ~Qt::ShiftModifier); break; } QAbstractSpinBox::keyPressEvent(e); if (select && !(e->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) << e->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 *e){ Q_D(QDateTimeEdit); int fw = d->frame ? style()->pixelMetric(QStyle::PM_SpinBoxFrameWidth) : 0; QPoint pnt(e->pos() - QPoint(fw, fw)); pnt.rx() -= d->edit->x(); int index = d->edit->cursorPositionAt(pnt); int s = d->closestSection(index, d->edit->cursorPosition() > index); // should it be > pos? if (s != d->currentSectionIndex) { d->edit->setCursorPosition(d->sectionPos(s)); QDTEDEBUG << d->sectionPos(s); } switch (d->sectionType(s)) { case QDateTimeParser::NoSection: case QDateTimeParser::FirstSection: case QDateTimeParser::LastSection: break; default: QAbstractSpinBox::wheelEvent(e); break; }}#endif/*! \reimp*/void QDateTimeEdit::focusInEvent(QFocusEvent *e){ Q_D(QDateTimeEdit); QAbstractSpinBox::focusInEvent(e); QString *frm = 0; if (d->displayFormat == d->defaultTimeFormat) { frm = &d->defaultTimeFormat; } else if (d->displayFormat == d->defaultDateFormat) { frm = &d->defaultDateFormat; } if (frm) { d->readLocaleSettings(); setDisplayFormat(*frm); } bool first; switch (e->reason()) { case Qt::ShortcutFocusReason: case Qt::TabFocusReason: first = true; break; case Qt::BacktabFocusReason: first = false; break; default: return; } if (QApplication::isRightToLeft()) first = !first; d->setSelected(first ? 0 : d->sectionNodes.size() - 1);}/*! \reimp*/bool QDateTimeEdit::focusNextPrevChild(bool next){ Q_D(QDateTimeEdit); if (!focusWidget())
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -