From c901cdadc0a6ec65eddfe4f181274e56567f9973 Mon Sep 17 00:00:00 2001 From: Andy Shaw Date: Wed, 30 May 2018 20:58:24 +0200 Subject: 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 --- .../widgets/widgets/qlineedit/tst_qlineedit.cpp | 41 +++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'tests/auto/widgets/widgets') 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" -- cgit v1.2.3