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>bb10
parent
e3e0a6d6d6
commit
c901cdadc0
|
|
@ -1673,6 +1673,16 @@ void QLineEdit::mouseDoubleClickEvent(QMouseEvent* e)
|
|||
the inputMask() and the validator() returns QValidator::Acceptable.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\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.
|
||||
|
||||
|
|
|
|||
|
|
@ -207,6 +207,7 @@ Q_SIGNALS:
|
|||
void returnPressed();
|
||||
void editingFinished();
|
||||
void selectionChanged();
|
||||
void inputRejected();
|
||||
|
||||
protected:
|
||||
void mousePressEvent(QMouseEvent *) override;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -545,6 +545,7 @@ Q_SIGNALS:
|
|||
void accepted();
|
||||
void editingFinished();
|
||||
void updateNeeded(const QRect &);
|
||||
void inputRejected();
|
||||
|
||||
#ifdef QT_KEYPAD_NAVIGATION
|
||||
void editFocusChange(bool);
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
Loading…
Reference in New Issue