📄 qabstractspinbox.cpp
字号:
}#endif/*! \reimp*/void QAbstractSpinBox::focusInEvent(QFocusEvent *e){ Q_D(QAbstractSpinBox); d->edit->event(e); if (e->reason() == Qt::TabFocusReason || e->reason() == Qt::BacktabFocusReason) { selectAll(); } QWidget::focusInEvent(e);}/*! \reimp*/void QAbstractSpinBox::focusOutEvent(QFocusEvent *e){ Q_D(QAbstractSpinBox); if (d->pendingEmit) d->interpret(EmitIfChanged); d->reset(); d->edit->event(e); QWidget::focusOutEvent(e); emit editingFinished();}/*! \reimp*/void QAbstractSpinBox::closeEvent(QCloseEvent *e){ Q_D(QAbstractSpinBox); d->reset(); if (d->pendingEmit) d->interpret(EmitIfChanged); QWidget::closeEvent(e);}/*! \reimp*/void QAbstractSpinBox::hideEvent(QHideEvent *e){ Q_D(QAbstractSpinBox); d->reset(); if (d->pendingEmit) d->interpret(EmitIfChanged); QWidget::hideEvent(e);}/*! \reimp*/void QAbstractSpinBox::timerEvent(QTimerEvent *e){ Q_D(QAbstractSpinBox); bool doStep = false; if (e->timerId() == d->spinClickThresholdTimerId) { killTimer(d->spinClickThresholdTimerId); d->spinClickThresholdTimerId = -1; d->spinClickTimerId = startTimer(d->spinClickTimerInterval); doStep = true; } else if (e->timerId() == d->spinClickTimerId) { 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); } } } QWidget::timerEvent(e); return;}/*! \reimp*/void QAbstractSpinBox::contextMenuEvent(QContextMenuEvent *e){#ifdef QT_NO_MENU Q_UNUSED(e);#else Q_D(QAbstractSpinBox); d->reset(); QPointer<QMenu> menu = d->edit->createStandardContextMenu(); 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); const QPointer<QAbstractSpinBox> that = this; const QPoint pos = (e->reason() == QContextMenuEvent::Mouse) ? e->globalPos() : mapToGlobal(QPoint(e->pos().x(), 0)) + QPoint(width() / 2, height() / 2); const QAction *action = menu->exec(pos); delete static_cast<QMenu *>(menu); if (that) { if (action == up) { stepBy(1); } else if (action == down) { stepBy(-1); } } e->accept();#endif // QT_NO_MENU}/*! \reimp*/void QAbstractSpinBox::mouseMoveEvent(QMouseEvent *e){ Q_D(QAbstractSpinBox); d->updateHoverControl(e->pos()); // If we have a timer ID, update the state const StepEnabled se = stepEnabled(); if (d->spinClickTimerId != -1) { 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(); e->accept(); }}/*! \reimp*/void QAbstractSpinBox::mousePressEvent(QMouseEvent *e){ Q_D(QAbstractSpinBox); if (e->button() != Qt::LeftButton || d->buttonState != None) { return; } d->updateHoverControl(e->pos()); e->accept(); 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 { e->ignore(); }}/*! \reimp*/void QAbstractSpinBox::mouseReleaseEvent(QMouseEvent *e){ Q_D(QAbstractSpinBox); if ((d->buttonState & Mouse) != 0) d->reset(); e->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("\x01"), cachedState(QValidator::Invalid), pendingEmit(false), readOnly(false), wrapping(false), ignoreCursorPositionChanged(false), frame(true), 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 = getStyleOption(); 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;}/*! \internal Returns true if a specialValueText has been set and the current value is minimum.*/bool QAbstractSpinBoxPrivate::specialValue() const{ return (value == minimum && specialValueText.size() > 0);}/*! \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); 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; }}/*! \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); spinClickTimerInterval = q->style()->styleHint(QStyle::SH_SpinBox_ClickAutoRepeatRate, 0, q); spinClickThresholdTimerInterval = thresholdTime; q->setFocusPolicy(Qt::WheelFocus); q->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed); q->setAttribute(Qt::WA_InputMethodEnabled); q->setLineEdit(new QLineEdit(q)); edit->setObjectName(QLatin1String("qt_spinbox_lineedit")); if (type != QVariant::Invalid) { validator = new QSpinBoxValidator(q, this); edit->setValidator(validator); }}/*! \internal Calls QWidget::update() on the area where the arrows are painted.*/void QAbstractSpinBoxPrivate::updateButtons(){ Q_Q(QAbstractSpinBox); if (q) { QStyleOptionSpinBox opt = getStyleOption(); q->update(q->style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxUp, q)); q->update(q->style()->subControlRect(QStyle::CC_SpinBox, &opt, QStyle::SC_SpinBoxDown, q)); }}/*! \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; updateButtons(); }}/*! \internal Updates the state of the spinbox.*/void QAbstractSpinBoxPrivate::updateState(bool up){ Q_Q(QAbstractSpinBox); if ((up && (buttonState & Up)) || (!up && (buttonState & Down))) return; reset(); if (q && (q->stepEnabled() & (up ? QAbstractSpinBox::StepUpEnabled : QAbstractSpinBox::StepDownEnabled))) { spinClickThresholdTimerId = q->startTimer(spinClickThresholdTimerInterval); buttonState = (up ? (Mouse | Up) : (Mouse | Down)); q->stepBy(up ? 1 : -1); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -