Skip to content

Commit

Permalink
QAbstractSpinBox: Clear 'cleared' flag on receiving a keypress
Browse files Browse the repository at this point in the history
Prevent QAbstractSpinBoxPrivate::interpret() from bailing out
in focus changes after text has been entered.

Task-number: QTBUG-55249
Change-Id: I250b3c50f7db5de2e9356038df20f18ee059df11
Reviewed-by: Maurice Kalinowski <[email protected]>
  • Loading branch information
FriedemannKleint authored and mauricek committed Aug 25, 2016
1 parent c0a513d commit 1e85ca2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/widgets/widgets/qabstractspinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,8 @@ void QAbstractSpinBox::keyPressEvent(QKeyEvent *event)
}

d->edit->event(event);
if (!d->edit->text().isEmpty())
d->cleared = false;
if (!isVisible())
d->ignoreUpdateEdit = true;
}
Expand Down
29 changes: 29 additions & 0 deletions tests/auto/widgets/widgets/qspinbox/tst_qspinbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ private slots:

void positiveSign();

void interpretOnLosingFocus();

void setGroupSeparatorShown_data();
void setGroupSeparatorShown();

Expand Down Expand Up @@ -1150,6 +1152,33 @@ void tst_QSpinBox::positiveSign()
QCOMPARE(spinBox.text(), QLatin1String("+20"));
}

void tst_QSpinBox::interpretOnLosingFocus()
{
// QTBUG-55249: When typing an invalid value after QSpinBox::clear(),
// it should be fixed up on losing focus.

static const int minimumValue = 10;
static const int maximumValue = 20;

QWidget widget;
widget.setWindowTitle(QTest::currentTestFunction());
QVBoxLayout *layout = new QVBoxLayout(&widget);
QLineEdit *focusDummy = new QLineEdit("focusDummy", &widget);
layout->addWidget(focusDummy);
SpinBox *spinBox = new SpinBox(&widget);
spinBox->setRange(minimumValue, maximumValue);
spinBox->setValue(minimumValue);
layout->addWidget(spinBox);
spinBox->clear();
spinBox->setFocus();
widget.show();
QVERIFY(QTest::qWaitForWindowActive(&widget));
QTest::keyClick(spinBox, Qt::Key_1); // Too small
focusDummy->setFocus();
QCOMPARE(spinBox->value(), minimumValue);
QCOMPARE(spinBox->lineEdit()->text().toInt(), minimumValue);
}

void tst_QSpinBox::setGroupSeparatorShown_data()
{
QTest::addColumn<QLocale::Language>("lang");
Expand Down

0 comments on commit 1e85ca2

Please sign in to comment.