summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy Shaw <andy.shaw@qt.io>2018-05-30 20:58:24 +0200
committerAndy Shaw <andy.shaw@qt.io>2018-06-01 21:34:03 +0000
commitc901cdadc0a6ec65eddfe4f181274e56567f9973 (patch)
tree0cd722d395efc2b96a394ac862f1cda6ff61a9c3
parente3e0a6d6d6c50dc21439f24728ee3614c5825d36 (diff)
QLineEdit: Add an inputRejected() signal for when a key is not allowed
[ChangeLog][QtWidgets][QLineEdit] Added inputRejected() signal for when a key press is not accepted by the QLineEdit. For instance, when an invalid key is pressed for a validator set. Task-number: QTBUG-57448 Change-Id: I39182a78b07b37c6da01905b8da4c57930e3454b Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
-rw-r--r--src/widgets/widgets/qlineedit.cpp10
-rw-r--r--src/widgets/widgets/qlineedit.h1
-rw-r--r--src/widgets/widgets/qlineedit_p.cpp1
-rw-r--r--src/widgets/widgets/qwidgetlinecontrol.cpp8
-rw-r--r--src/widgets/widgets/qwidgetlinecontrol_p.h1
-rw-r--r--tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp41
6 files changed, 61 insertions, 1 deletions
diff --git a/src/widgets/widgets/qlineedit.cpp b/src/widgets/widgets/qlineedit.cpp
index 9e70b54601..43aa10bdb6 100644
--- a/src/widgets/widgets/qlineedit.cpp
+++ b/src/widgets/widgets/qlineedit.cpp
@@ -1674,6 +1674,16 @@ void QLineEdit::mouseDoubleClickEvent(QMouseEvent* e)
*/
/*!
+ \fn void QLineEdit::inputRejected()
+
+ This signal is emitted when the user presses a key that is not
+ considered to be acceptable input. For example, if a key press
+ results in a validator's validate() call to return Invalid.
+ Another case is when trying to enter in more characters beyond the
+ maximum length of the line edit.
+*/
+
+/*!
Converts the given key press \a event into a line edit action.
If Return or Enter is pressed and the current text is valid (or
diff --git a/src/widgets/widgets/qlineedit.h b/src/widgets/widgets/qlineedit.h
index 099bf4bb18..b3be97e9b7 100644
--- a/src/widgets/widgets/qlineedit.h
+++ b/src/widgets/widgets/qlineedit.h
@@ -207,6 +207,7 @@ Q_SIGNALS:
void returnPressed();
void editingFinished();
void selectionChanged();
+ void inputRejected();
protected:
void mousePressEvent(QMouseEvent *) override;
diff --git a/src/widgets/widgets/qlineedit_p.cpp b/src/widgets/widgets/qlineedit_p.cpp
index acf49cda72..bfb92468be 100644
--- a/src/widgets/widgets/qlineedit_p.cpp
+++ b/src/widgets/widgets/qlineedit_p.cpp
@@ -214,6 +214,7 @@ void QLineEditPrivate::init(const QString& txt)
QObject::connect(control, SIGNAL(updateNeeded(QRect)),
q, SLOT(_q_updateNeeded(QRect)));
+ QObject::connect(control, SIGNAL(inputRejected()), q, SIGNAL(inputRejected()));
QStyleOptionFrame opt;
q->initStyleOption(&opt);
diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp
index ff00e26683..670735df3b 100644
--- a/src/widgets/widgets/qwidgetlinecontrol.cpp
+++ b/src/widgets/widgets/qwidgetlinecontrol.cpp
@@ -715,6 +715,8 @@ bool QWidgetLineControl::finishChange(int validateFromState, bool update, bool e
return true;
}
m_cursor = cursorCopy;
+ } else {
+ emit inputRejected();
}
}
#endif
@@ -762,6 +764,8 @@ void QWidgetLineControl::internalSetText(const QString &txt, int pos, bool edite
if (m_maskData) {
m_text = maskString(0, txt, true);
m_text += clearString(m_text.length(), m_maxLength - m_text.length());
+ if (edited && oldText == m_text)
+ emit inputRejected();
} else {
m_text = txt.isEmpty() ? txt : txt.left(m_maxLength);
}
@@ -839,6 +843,8 @@ void QWidgetLineControl::internalInsert(const QString &s)
addCommand(Command(SetSelection, m_cursor, 0, m_selstart, m_selend));
if (m_maskData) {
QString ms = maskString(m_cursor, s);
+ if (ms.isEmpty() && !s.isEmpty())
+ emit inputRejected();
#ifndef QT_NO_ACCESSIBILITY
QAccessibleTextInsertEvent insertEvent(accessibleObject(), m_cursor, ms);
QAccessible::updateAccessibility(&insertEvent);
@@ -866,6 +872,8 @@ void QWidgetLineControl::internalInsert(const QString &s)
for (int i = 0; i < (int) s.left(remaining).length(); ++i)
addCommand(Command(Insert, m_cursor++, s.at(i), -1, -1));
m_textDirty = true;
+ } else {
+ emit inputRejected();
}
}
}
diff --git a/src/widgets/widgets/qwidgetlinecontrol_p.h b/src/widgets/widgets/qwidgetlinecontrol_p.h
index ca70e2c02f..3e33bc0605 100644
--- a/src/widgets/widgets/qwidgetlinecontrol_p.h
+++ b/src/widgets/widgets/qwidgetlinecontrol_p.h
@@ -545,6 +545,7 @@ Q_SIGNALS:
void accepted();
void editingFinished();
void updateNeeded(const QRect &);
+ void inputRejected();
#ifdef QT_KEYPAD_NAVIGATION
void editFocusChange(bool);
diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp
index 7eb739611a..6e472bff02 100644
--- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp
+++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp
@@ -304,7 +304,7 @@ private slots:
void QTBUG59957_clearButtonLeftmostAction();
void QTBUG_60319_setInputMaskCheckImSurroundingText();
void testQuickSelectionWithMouse();
-
+ void inputRejected();
protected slots:
void editingFinished();
@@ -4820,5 +4820,44 @@ void tst_QLineEdit::testQuickSelectionWithMouse()
QVERIFY(lineEdit.selectedText().endsWith(suffix));
}
+void tst_QLineEdit::inputRejected()
+{
+ QLineEdit *testWidget = ensureTestWidget();
+ QSignalSpy spyInputRejected(testWidget, SIGNAL(inputRejected()));
+
+ QTest::keyClicks(testWidget, "abcde");
+ QCOMPARE(spyInputRejected.count(), 0);
+ testWidget->setText("fghij");
+ QCOMPARE(spyInputRejected.count(), 0);
+ testWidget->insert("k");
+ QCOMPARE(spyInputRejected.count(), 0);
+
+ testWidget->clear();
+ testWidget->setMaxLength(5);
+ QTest::keyClicks(testWidget, "abcde");
+ QCOMPARE(spyInputRejected.count(), 0);
+ QTest::keyClicks(testWidget, "fgh");
+ QCOMPARE(spyInputRejected.count(), 3);
+
+ testWidget->setMaxLength(INT_MAX);
+ testWidget->clear();
+ spyInputRejected.clear();
+ QIntValidator intValidator(1, 100);
+ testWidget->setValidator(&intValidator);
+ QTest::keyClicks(testWidget, "11");
+ QCOMPARE(spyInputRejected.count(), 0);
+ QTest::keyClicks(testWidget, "a#");
+ QCOMPARE(spyInputRejected.count(), 2);
+
+ testWidget->clear();
+ testWidget->setValidator(0);
+ spyInputRejected.clear();
+ testWidget->setInputMask("999.999.999.999;_");
+ QTest::keyClicks(testWidget, "11");
+ QCOMPARE(spyInputRejected.count(), 0);
+ QTest::keyClicks(testWidget, "a#");
+ QCOMPARE(spyInputRejected.count(), 2);
+}
+
QTEST_MAIN(tst_QLineEdit)
#include "tst_qlineedit.moc"