Don't clear lineedit in non normal echo mode when validation is invalid

This fixes a regression introduced with
c09e9f71173a698670d6c728291ee24f53d50800 which caused the lineedit to
clear the whole text when an invalid character was entered into a lineedit
with an echo mode that was not Normal and a validator was set.

Now if undo() is called directly then it will still clear the text as it
is considered to be called as a user. Whereas the validation will take
care of the invalid entry by using internalUndo() as before which avoids
the clearing of the entire text.

Task-number: QTBUG-29318
Change-Id: I5ff5777a75ab864de2217441b5f518f50646bd8f
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com>
Reviewed-by: Giuseppe D'Angelo <giuseppe.dangelo@kdab.com>
bb10
Andy Shaw 2014-12-05 10:22:28 +01:00
parent 7393bb0af4
commit a07120d496
3 changed files with 55 additions and 21 deletions

View File

@ -279,6 +279,23 @@ void QWidgetLineControl::clear()
separate();
finishChange(priorState, /*update*/false, /*edited*/false);
}
/*!
\internal
Undoes the previous operation.
*/
void QWidgetLineControl::undo()
{
// Undo works only for clearing the line when in any of password the modes
if (m_echoMode == QLineEdit::Normal) {
internalUndo();
finishChange(-1, true);
} else {
cancelPasswordEchoTimer();
clear();
}
}
/*!
\internal
@ -1278,12 +1295,6 @@ void QWidgetLineControl::internalUndo(int until)
cancelPasswordEchoTimer();
internalDeselect();
// Undo works only for clearing the line when in any of password the modes
if (m_echoMode != QLineEdit::Normal) {
clear();
return;
}
while (m_undoState && m_undoState > until) {
Command& cmd = m_history[--m_undoState];
switch (cmd.type) {

View File

@ -245,7 +245,7 @@ public:
void insert(const QString &);
void clear();
void undo() { internalUndo(); finishChange(-1, true); }
void undo();
void redo() { internalRedo(); finishChange(); }
void selectWordAtPos(int);

View File

@ -2512,6 +2512,7 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
QTest::addColumn<QString>("expectedText");
QTest::addColumn<bool>("useKeys");
QTest::addColumn<bool>("is_valid");
QTest::addColumn<uint>("echoMode");
for (int i=0; i<2; i++) {
bool useKeys = false;
@ -2528,7 +2529,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("1")
<< QString("1")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [3,7] valid '3'").toLatin1())
<< 3
@ -2536,7 +2538,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("3")
<< QString("3")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [3,7] valid '7'").toLatin1())
<< 3
@ -2544,7 +2547,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("7")
<< QString("7")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [0,100] valid '9'").toLatin1())
<< 0
@ -2552,7 +2556,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("9")
<< QString("9")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [0,100] valid '12'").toLatin1())
<< 0
@ -2560,7 +2565,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("12")
<< QString("12")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [-100,100] valid '-12'").toLatin1())
<< -100
@ -2568,7 +2574,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("-12")
<< QString("-12")
<< bool(useKeys)
<< bool(true);
<< bool(true)
<< uint(QLineEdit::Normal);
// invalid data
// characters not allowed in QIntValidator
@ -2578,7 +2585,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("a")
<< QString("")
<< bool(useKeys)
<< bool(false);
<< bool(false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [0,9] inv 'A'").toLatin1())
<< 0
@ -2586,7 +2594,8 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("A")
<< QString("")
<< bool(useKeys)
<< bool(false);
<< bool(false)
<< uint(QLineEdit::Normal);
// minus sign only allowed with a range on the negative side
QTest::newRow(QString(inputMode + "range [0,100] inv '-'").toLatin1())
<< 0
@ -2594,36 +2603,48 @@ void tst_QLineEdit::setValidator_QIntValidator_data()
<< QString("-")
<< QString("")
<< bool(useKeys)
<< bool(false);
<< bool(false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [0,100] int '153'").toLatin1())
<< 0
<< 100
<< QString("153")
<< QString(useKeys ? "15" : "")
<< bool(useKeys)
<< bool(useKeys ? true : false);
<< bool(useKeys ? true : false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [-100,100] int '-153'").toLatin1())
<< -100
<< 100
<< QString("-153")
<< QString(useKeys ? "-15" : "")
<< bool(useKeys)
<< bool(useKeys ? true : false);
<< bool(useKeys ? true : false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [3,7] int '2'").toLatin1())
<< 3
<< 7
<< QString("2")
<< QString("2")
<< bool(useKeys)
<< bool(false);
<< bool(false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [3,7] int '8'").toLatin1())
<< 3
<< 7
<< QString("8")
<< QString("")
<< bool(useKeys)
<< bool(false);
<< bool(false)
<< uint(QLineEdit::Normal);
QTest::newRow(QString(inputMode + "range [0,99] inv 'a-a'").toLatin1())
<< 0
<< 99
<< QString("19a")
<< QString(useKeys ? "19" : "")
<< bool(useKeys)
<< bool(useKeys ? true : false)
<< uint(QLineEdit::Password);
}
}
@ -2635,9 +2656,11 @@ void tst_QLineEdit::setValidator_QIntValidator()
QFETCH(QString, expectedText);
QFETCH(bool, useKeys);
QFETCH(bool, is_valid);
QFETCH(uint, echoMode);
QIntValidator intValidator(mini, maxi, 0);
QLineEdit *testWidget = ensureTestWidget();
testWidget->setEchoMode((QLineEdit::EchoMode)echoMode);
testWidget->setValidator(&intValidator);
QVERIFY(testWidget->text().isEmpty());
//qDebug("1 input: '" + input + "' Exp: '" + expectedText + "'");