Maintain keyboard modifier state in QAbstractSpinBox's input event handlers

Gets rid of calls to QGuiApplication::keyboardModifiers. Need to handle
key and mouse events, since the spin buttons may be clicked on without
the spinbox having focus.

Task-number: QTBUG-73829
Change-Id: I455c42987f19bb5b7997dc8d61272863d7bc394e
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
bb10
Volker Hilsheimer 2020-07-02 12:10:13 +02:00
parent 2a6cdec718
commit 117a5aa744
2 changed files with 10 additions and 3 deletions

View File

@ -1010,6 +1010,8 @@ void QAbstractSpinBox::keyPressEvent(QKeyEvent *event)
{
Q_D(QAbstractSpinBox);
d->keyboardModifiers = event->modifiers();
if (!event->text().isEmpty() && d->edit->cursorPosition() < d->prefix.size())
d->edit->setCursorPosition(d->prefix.size());
@ -1150,6 +1152,7 @@ void QAbstractSpinBox::keyReleaseEvent(QKeyEvent *event)
{
Q_D(QAbstractSpinBox);
d->keyboardModifiers = event->modifiers();
if (d->buttonState & Keyboard && !event->isAutoRepeat()) {
d->reset();
} else {
@ -1282,7 +1285,7 @@ void QAbstractSpinBox::timerEvent(QTimerEvent *event)
}
if (doStep) {
const bool increaseStepRate = QGuiApplication::keyboardModifiers() & d->stepModifier;
const bool increaseStepRate = d->keyboardModifiers & d->stepModifier;
const StepEnabled st = stepEnabled();
if (d->buttonState & Up) {
if (!(st & StepUpEnabled)) {
@ -1359,6 +1362,7 @@ void QAbstractSpinBox::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QAbstractSpinBox);
d->keyboardModifiers = event->modifiers();
d->updateHoverControl(event->position().toPoint());
// If we have a timer ID, update the state
@ -1382,6 +1386,7 @@ void QAbstractSpinBox::mousePressEvent(QMouseEvent *event)
{
Q_D(QAbstractSpinBox);
d->keyboardModifiers = event->modifiers();
if (event->button() != Qt::LeftButton || d->buttonState != None) {
return;
}
@ -1406,6 +1411,7 @@ void QAbstractSpinBox::mouseReleaseEvent(QMouseEvent *event)
{
Q_D(QAbstractSpinBox);
d->keyboardModifiers = event->modifiers();
if ((d->buttonState & Mouse) != 0)
d->reset();
event->accept();
@ -1421,7 +1427,7 @@ void QAbstractSpinBox::mouseReleaseEvent(QMouseEvent *event)
QAbstractSpinBoxPrivate::QAbstractSpinBoxPrivate()
: edit(nullptr), type(QMetaType::UnknownType), spinClickTimerId(-1),
spinClickTimerInterval(100), spinClickThresholdTimerId(-1), spinClickThresholdTimerInterval(-1),
effectiveSpinRepeatRate(1), buttonState(None), cachedText(QLatin1String("\x01")),
effectiveSpinRepeatRate(1), buttonState(None), keyboardModifiers{}, cachedText(QLatin1String("\x01")),
cachedState(QValidator::Invalid), pendingEmit(false), readOnly(false), wrapping(false),
ignoreCursorPositionChanged(false), frame(true), accelerate(false), keyboardTracking(true),
cleared(false), ignoreUpdateEdit(false), correctionMode(QAbstractSpinBox::CorrectToPreviousValue),
@ -1679,7 +1685,7 @@ void QAbstractSpinBoxPrivate::updateState(bool up, bool fromKeyboard /* = false
spinClickThresholdTimerId = q->startTimer(spinClickThresholdTimerInterval);
buttonState = (up ? Up : Down) | (fromKeyboard ? Keyboard : Mouse);
int steps = up ? 1 : -1;
if (QGuiApplication::keyboardModifiers() & stepModifier)
if (keyboardModifiers & stepModifier)
steps *= 10;
q->stepBy(steps);
#ifndef QT_NO_ACCESSIBILITY

View File

@ -131,6 +131,7 @@ public:
int spinClickTimerId, spinClickTimerInterval, spinClickThresholdTimerId, spinClickThresholdTimerInterval;
int effectiveSpinRepeatRate;
uint buttonState;
Qt::KeyboardModifiers keyboardModifiers;
mutable QString cachedText;
mutable QVariant cachedValue;
mutable QValidator::State cachedState;