From: Andrew den Exter Date: Tue, 10 Jan 2012 06:51:22 +0000 (+1000) Subject: Fix validators not allowing backspace or delete. X-Git-Tag: upstream/5.2.1~2919 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=0cafd920cced1f726a8b6422b6fb496ecf846f34;p=platform%2Fupstream%2Fqtdeclarative.git Fix validators not allowing backspace or delete. TextInput will allow non-valid changes to already invalid text, when a validator is changed update the current validity so changes can be made to bring the text back to a valid state. Task-number: QTBUG-22080 Change-Id: I501961b473f58c317bce474b1df8d91fd1f967d4 Reviewed-by: Martin Jones --- diff --git a/src/quick/items/qquicktextinput.cpp b/src/quick/items/qquicktextinput.cpp index df536f4..12f02d8 100644 --- a/src/quick/items/qquicktextinput.cpp +++ b/src/quick/items/qquicktextinput.cpp @@ -931,7 +931,12 @@ void QQuickTextInput::setValidator(QValidator* v) d->m_validator = v; if (!d->hasAcceptableInput(d->m_text)) { - d->oldValidity = false; + if (d->m_validInput) { + d->m_validInput = false; + emit acceptableInputChanged(); + } + } else if (!d->m_validInput) { + d->m_validInput = true; emit acceptableInputChanged(); } @@ -2345,7 +2350,6 @@ void QQuickTextInputPrivate::init() q, SLOT(q_canPasteChanged())); #endif // QT_NO_CLIPBOARD - oldValidity = hasAcceptableInput(m_text); lastSelectionStart = 0; lastSelectionEnd = 0; selectedTextColor = m_palette.color(QPalette::HighlightedText); diff --git a/src/quick/items/qquicktextinput_p_p.h b/src/quick/items/qquicktextinput_p_p.h index 03d825d..44df9f2 100644 --- a/src/quick/items/qquicktextinput_p_p.h +++ b/src/quick/items/qquicktextinput_p_p.h @@ -105,7 +105,6 @@ public: , inputMethodHints(Qt::ImhNone) , m_layoutDirection(Qt::LayoutDirectionAuto) , m_passwordCharacter(QLatin1Char('*')) - , oldValidity(false) , focused(false) , focusOnPress(true) , cursorVisible(false) @@ -231,7 +230,6 @@ public: QChar m_blank; QChar m_passwordCharacter; - bool oldValidity:1; bool focused:1; bool focusOnPress:1; bool cursorVisible:1; diff --git a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp index 017ac53..ac60ad3 100644 --- a/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp +++ b/tests/auto/qtquick2/qquicktextinput/tst_qquicktextinput.cpp @@ -1611,6 +1611,47 @@ void tst_qquicktextinput::validators() QTRY_COMPARE(dblInput->text(), QLatin1String("12.11")); QCOMPARE(dblInput->hasAcceptableInput(), true); + // Ensure the validator doesn't prevent characters being removed. + dblInput->setValidator(intInput->validator()); + QCOMPARE(dblInput->text(), QLatin1String("12.11")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_Backspace); + QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("12.1")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + // Once unacceptable input is in anything goes until it reaches an acceptable state again. + QTest::keyPress(&canvas, Qt::Key_1); + QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("12.11")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_Backspace); + QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("12.1")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_Backspace); + QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("12.")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_Backspace); + QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("12")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_Backspace); + QTest::keyRelease(&canvas, Qt::Key_Backspace, Qt::NoModifier ,10); + QTest::qWait(50); + QTRY_COMPARE(dblInput->text(), QLatin1String("1")); + QCOMPARE(dblInput->hasAcceptableInput(), false); + QTest::keyPress(&canvas, Qt::Key_1); + QTest::keyRelease(&canvas, Qt::Key_1, Qt::NoModifier ,10); + QTest::qWait(50); + QCOMPARE(dblInput->text(), QLatin1String("11")); + QCOMPARE(dblInput->hasAcceptableInput(), true); + QQuickTextInput *strInput = qobject_cast(qvariant_cast(canvas.rootObject()->property("strInput"))); QTRY_VERIFY(strInput); strInput->setFocus(true);