📄 qabstractspinbox.cpp
字号:
} d->edit->event(event);}/*! \reimp*/void QAbstractSpinBox::keyReleaseEvent(QKeyEvent *event){ Q_D(QAbstractSpinBox); if (d->buttonState & Keyboard && !event->isAutoRepeat() && style()->styleHint(QStyle::SH_SpinBox_AnimateButton, 0, this)) { d->reset(); } else { d->edit->event(event); }}/*! \reimp*/#ifndef QT_NO_WHEELEVENTvoid QAbstractSpinBox::wheelEvent(QWheelEvent *event){ const int steps = (event->delta() > 0 ? 1 : -1); if (stepEnabled() & (steps > 0 ? StepUpEnabled : StepDownEnabled)) stepBy(event->modifiers() & Qt::ControlModifier ? steps * 10 : steps); event->accept();}#endif/*! \reimp*/void QAbstractSpinBox::focusInEvent(QFocusEvent *event){ Q_D(QAbstractSpinBox); d->edit->event(event); if (event->reason() == Qt::TabFocusReason || event->reason() == Qt::BacktabFocusReason) { selectAll(); } QWidget::focusInEvent(event);}/*! \reimp*/void QAbstractSpinBox::focusOutEvent(QFocusEvent *event){ Q_D(QAbstractSpinBox); if (d->pendingEmit) d->interpret(EmitIfChanged); d->reset(); d->edit->event(event); QWidget::focusOutEvent(event); emit editingFinished();}/*! \reimp*/void QAbstractSpinBox::closeEvent(QCloseEvent *event){ Q_D(QAbstractSpinBox); d->reset(); if (d->pendingEmit) d->interpret(EmitIfChanged); QWidget::closeEvent(event);}/*! \reimp*/void QAbstractSpinBox::hideEvent(QHideEvent *event){ Q_D(QAbstractSpinBox); d->reset(); if (d->pendingEmit) d->interpret(EmitIfChanged); QWidget::hideEvent(event);}/*! \reimp*/void QAbstractSpinBox::timerEvent(QTimerEvent *event){ Q_D(QAbstractSpinBox); bool doStep = false; if (event->timerId() == d->spinClickThresholdTimerId) { killTimer(d->spinClickThresholdTimerId); d->spinClickThresholdTimerId = -1; d->spinClickTimerId = startTimer(d->spinClickTimerInterval); doStep = true; } else if (event->timerId() == d->spinClickTimerId) { if (d->accelerate) { d->acceleration = d->acceleration + (int)(d->spinClickTimerInterval * 0.05); if (d->spinClickTimerInterval - d->acceleration >= 10) { killTimer(d->spinClickTimerId); d->spinClickTimerId = startTimer(d->spinClickTimerInterval - d->acceleration); } } doStep = true; } if (doStep) { const StepEnabled st = stepEnabled(); if (d->buttonState & Up) { if (!(st & StepUpEnabled)) { d->reset(); } else { stepBy(1); } } else if (d->buttonState & Down) { if (!(st & StepDownEnabled)) { d->reset(); } else { stepBy(-1); } } return; } QWidget::timerEvent(event); return;}/*! \reimp*/void QAbstractSpinBox::contextMenuEvent(QContextMenuEvent *event){#ifdef QT_NO_CONTEXTMENU Q_UNUSED(event);#else Q_D(QAbstractSpinBox); d->reset(); QPointer<QMenu> menu = d->edit->createStandardContextMenu(); QAction *selAll = new QAction(tr("&Select All"), menu); menu->insertAction(d->edit->d_func()->actions[QLineEditPrivate::SelectAllAct], selAll); menu->removeAction(d->edit->d_func()->actions[QLineEditPrivate::SelectAllAct]); menu->addSeparator(); const uint se = stepEnabled(); QAction *up = menu->addAction(tr("&Step up")); up->setEnabled(se & StepUpEnabled); QAction *down = menu->addAction(tr("Step &down")); down->setEnabled(se & StepDownEnabled); menu->addSeparator(); const QPointer<QAbstractSpinBox> that = this; const QPoint pos = (event->reason() == QContextMenuEvent::Mouse) ? event->globalPos() : mapToGlobal(QPoint(event->pos().x(), 0)) + QPoint(width() / 2, height() / 2); const QAction *action = menu->exec(pos); delete static_cast<QMenu *>(menu); if (that && action) { if (action == up) { stepBy(1); } else if (action == down) { stepBy(-1); } else if (action == selAll) { selectAll(); } } event->accept();#endif // QT_NO_CONTEXTMENU}/*! \reimp*/void QAbstractSpinBox::mouseMoveEvent(QMouseEvent *event){ Q_D(QAbstractSpinBox); d->updateHoverControl(event->pos()); // If we have a timer ID, update the state if (d->spinClickTimerId != -1 && d->buttonSymbols != NoButtons) { const StepEnabled se = stepEnabled(); if ((se & StepUpEnabled) && d->hoverControl == QStyle::SC_SpinBoxUp) d->updateState(true); else if ((se & StepDownEnabled) && d->hoverControl == QStyle::SC_SpinBoxDown) d->updateState(false); else d->reset(); event->accept(); }}/*! \reimp*/void QAbstractSpinBox::mousePressEvent(QMouseEvent *event){ Q_D(QAbstractSpinBox); if (event->button() != Qt::LeftButton || d->buttonState != None) { return; } d->updateHoverControl(event->pos()); event->accept(); const StepEnabled se = (d->buttonSymbols == NoButtons) ? StepEnabled(StepNone) : stepEnabled(); if ((se & StepUpEnabled) && d->hoverControl == QStyle::SC_SpinBoxUp) { d->updateState(true); } else if ((se & StepDownEnabled) && d->hoverControl == QStyle::SC_SpinBoxDown) { d->updateState(false); } else { event->ignore(); }}/*! \reimp*/void QAbstractSpinBox::mouseReleaseEvent(QMouseEvent *event){ Q_D(QAbstractSpinBox); if ((d->buttonState & Mouse) != 0) d->reset(); event->accept();}// --- QAbstractSpinBoxPrivate ---/*! \internal Constructs a QAbstractSpinBoxPrivate object*/QAbstractSpinBoxPrivate::QAbstractSpinBoxPrivate() : edit(0), type(QVariant::Invalid), spinClickTimerId(-1), spinClickTimerInterval(100), spinClickThresholdTimerId(-1), spinClickThresholdTimerInterval(thresholdTime), buttonState(None), cachedText(QLatin1String("\x01")), cachedState(QValidator::Invalid), pendingEmit(false), readOnly(false), wrapping(false), ignoreCursorPositionChanged(false), frame(true), accelerate(false), keyboardTracking(true), correctionMode(QAbstractSpinBox::CorrectToPreviousValue), acceleration(0), hoverControl(QStyle::SC_None), buttonSymbols(QAbstractSpinBox::UpDownArrows), validator(0){}/* \internal Called when the QAbstractSpinBoxPrivate is destroyed*/QAbstractSpinBoxPrivate::~QAbstractSpinBoxPrivate(){}/*! \internal Updates the old and new hover control. Does nothing if the hover control has not changed.*/bool QAbstractSpinBoxPrivate::updateHoverControl(const QPoint &pos){ Q_Q(QAbstractSpinBox); QRect lastHoverRect = hoverRect; QStyle::SubControl lastHoverControl = hoverControl; bool doesHover = q->testAttribute(Qt::WA_Hover); if (lastHoverControl != newHoverControl(pos) && doesHover) { q->update(lastHoverRect); q->update(hoverRect); return true; } return !doesHover;}/*! \internal Returns the hover control at \a pos. This will update the hoverRect and hoverControl.*/QStyle::SubControl QAbstractSpinBoxPrivate::newHoverControl(const QPoint &pos){ Q_Q(QAbstractSpinBox); QStyleOptionSpinBox opt; q->initStyleOption(&opt); opt.subControls = QStyle::SC_All; hoverControl = q->style()->hitTestComplexControl(QStyle::CC_SpinBox, &opt, pos, q); hoverRect = q->style()->subControlRect(QStyle::CC_SpinBox, &opt, hoverControl, q); return hoverControl;}/*! \internal Strips any prefix/suffix from \a text.*/QString QAbstractSpinBoxPrivate::stripped(const QString &t, int *pos) const{ QString text = t; if (specialValueText.size() == 0 || text != specialValueText) { int from = 0; int size = text.size(); bool changed = false; if (prefix.size() && text.startsWith(prefix)) { from += prefix.size(); size -= from; changed = true; } if (suffix.size() && text.endsWith(suffix)) { size -= suffix.size(); changed = true; } if (changed) text = text.mid(from, size); } const int s = text.size(); text = text.trimmed(); if (pos) (*pos) -= (s - text.size()); return text;}void QAbstractSpinBoxPrivate::updateEditFieldGeometry(){ Q_Q(QAbstractSpinBox); QStyleOptionSpinBox opt; q->initStyleOption(&opt); opt.subControls = QStyle::SC_SpinBoxEditField; edit->setGeometry(q->style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxEditField, q));}/*! \internal Returns true if a specialValueText has been set and the current value is minimum.*/bool QAbstractSpinBoxPrivate::specialValue() const{ return (value == minimum && !specialValueText.isEmpty());}/*! \internal Virtual function that emits signals when the value changes. Reimplemented in the different subclasses.*/void QAbstractSpinBoxPrivate::emitSignals(EmitPolicy, const QVariant &){}/*! \internal Slot connected to the line edit's textChanged(const QString &) signal.*/void QAbstractSpinBoxPrivate::_q_editorTextChanged(const QString &t){ Q_Q(QAbstractSpinBox); if (keyboardTracking) { QString tmp = t; int pos = edit->cursorPosition(); QValidator::State state = q->validate(tmp, pos); if (state == QValidator::Acceptable) { const QVariant v = valueFromText(tmp); setValue(v, EmitIfChanged, tmp != t); pendingEmit = false; } else { pendingEmit = true; } } else { pendingEmit = true; }}/*! \internal Virtual slot connected to the line edit's cursorPositionChanged(int, int) signal. Will move the cursor to a valid position if the new one is invalid. E.g. inside the prefix. Reimplemented in Q[Date|Time|DateTime]EditPrivate to account for the different sections etc.*/void QAbstractSpinBoxPrivate::_q_editorCursorPositionChanged(int oldpos, int newpos){ if (!edit->hasSelectedText() && !ignoreCursorPositionChanged && !specialValue()) { ignoreCursorPositionChanged = true; bool allowSelection = true; int pos = -1; if (newpos < prefix.size() && newpos != 0) { if (oldpos == 0) { allowSelection = false; pos = prefix.size(); } else { pos = oldpos; } } else if (newpos > edit->text().size() - suffix.size() && newpos != edit->text().size()) { if (oldpos == edit->text().size()) { pos = edit->text().size() - suffix.size(); allowSelection = false; } else { pos = edit->text().size(); } } if (pos != -1) { const int selSize = edit->selectionStart() >= 0 && allowSelection ? (edit->selectedText().size() * (newpos < pos ? -1 : 1)) - newpos + pos : 0; const bool wasBlocked = edit->blockSignals(true); if (selSize != 0) { edit->setSelection(pos - selSize, selSize); } else { edit->setCursorPosition(pos); } edit->blockSignals(wasBlocked); } ignoreCursorPositionChanged = false; }}/*! \internal Initialises the QAbstractSpinBoxPrivate object.*/void QAbstractSpinBoxPrivate::init(){ Q_Q(QAbstractSpinBox); QStyleOptionSpinBox opt; q->initStyleOption(&opt); spinClickTimerInterval = q->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatRate, &opt, q); spinClickThresholdTimerInterval = thresholdTime; q->setFocusPolicy(Qt::WheelFocus); q->setSizePolicy(QSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed, QSizePolicy::SpinBox)); q->setAttribute(Qt::WA_InputMethodEnabled); q->setLineEdit(new QLineEdit(q)); edit->setObjectName(QLatin1String("qt_spinbox_lineedit")); validator = new QSpinBoxValidator(q, this); edit->setValidator(validator); q->setAttribute(Qt::WA_MacShowFocusRect);}/*! \internal Resets the state of the spinbox. E.g. the state is set to (Keyboard|Up) if Key up is currently pressed.*/void QAbstractSpinBoxPrivate::reset(){ Q_Q(QAbstractSpinBox); buttonState = None; if (q) { if (spinClickTimerId != -1) q->killTimer(spinClickTimerId); if (spinClickThresholdTimerId != -1) q->killTimer(spinClickThresholdTimerId); spinClickTimerId = spinClickThresholdTimerId = -1; acceleration = 0; q->update(); }}/*! \internal Updates the state of the spinbox.*/void QAbstractSpinBoxPrivate::updateState(bool up){ Q_Q(QAbstractSpinBox); if ((up && (buttonState & Up)) || (!up && (buttonState & Down))) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -