From 872b9b231ef966ce7b2b40785ee92a0d4a8d2851 Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Tue, 8 Dec 2015 20:35:11 +0100 Subject: QLineEdit: fix the shortcut override events on a readonly line edit When a QLineEdit is readonly there's a discrepancy between key press events and shortcut override events. For instance, presses Ctrl+C copies the text unless there's also a shortcut for the same key sequence. In this case, the shortcut override event is not handled, and no text is copied. Fix it by splitting the handling of shortcut override events between "read only" access (copy, select, etc.), which still makes sense on a read only line edit, and write access (paste, ...) which doesn't. [ChangeLog][Important Behavior Changes][QLineEdit] QLineEdit will now accept certain shortcut override events even if it is read only. Change-Id: Ie5b048259b99a1eff0581129e3ad97f27a88fe86 Task-number: QTBUG-21217 Reviewed-by: Olivier Goffart (Woboq GmbH) Reviewed-by: Frederik Gladhorn --- .../widgets/widgets/qlineedit/tst_qlineedit.cpp | 60 ++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'tests/auto/widgets') diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index ba700ed58d..de896ea273 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -64,6 +64,7 @@ #include #include #include +#include #include "qcommonstyle.h" #include "qstyleoption.h" @@ -312,6 +313,9 @@ private slots: void shouldShowPlaceholderText(); void QTBUG1266_setInputMaskEmittingTextEdited(); + void shortcutOverrideOnReadonlyLineEdit_data(); + void shortcutOverrideOnReadonlyLineEdit(); + protected slots: void editingFinished(); @@ -4487,5 +4491,61 @@ void tst_QLineEdit::QTBUG1266_setInputMaskEmittingTextEdited() QCOMPARE(spy.count(), 0); } +void tst_QLineEdit::shortcutOverrideOnReadonlyLineEdit_data() +{ + QTest::addColumn("keySequence"); + QTest::addColumn("shouldBeHandledByQLineEdit"); + + QTest::newRow("Copy") << QKeySequence(QKeySequence::Copy) << true; + QTest::newRow("MoveToNextChar") << QKeySequence(QKeySequence::MoveToNextChar) << true; + QTest::newRow("SelectAll") << QKeySequence(QKeySequence::SelectAll) << true; + QTest::newRow("Right press") << QKeySequence(Qt::Key_Right) << true; + QTest::newRow("Left press") << QKeySequence(Qt::Key_Left) << true; + + QTest::newRow("Paste") << QKeySequence(QKeySequence::Paste) << false; + QTest::newRow("Paste") << QKeySequence(QKeySequence::Cut) << false; + QTest::newRow("Undo") << QKeySequence(QKeySequence::Undo) << false; + QTest::newRow("Redo") << QKeySequence(QKeySequence::Redo) << false; + + QTest::newRow("a") << QKeySequence(Qt::Key_A) << false; + QTest::newRow("b") << QKeySequence(Qt::Key_B) << false; + QTest::newRow("c") << QKeySequence(Qt::Key_C) << false; + QTest::newRow("x") << QKeySequence(Qt::Key_X) << false; + QTest::newRow("X") << QKeySequence(Qt::ShiftModifier + Qt::Key_X) << false; + + QTest::newRow("Alt+Home") << QKeySequence(Qt::AltModifier + Qt::Key_Home) << false; +} + +void tst_QLineEdit::shortcutOverrideOnReadonlyLineEdit() +{ + QFETCH(QKeySequence, keySequence); + QFETCH(bool, shouldBeHandledByQLineEdit); + + QWidget widget; + + QShortcut *shortcut = new QShortcut(keySequence, &widget); + QSignalSpy spy(shortcut, &QShortcut::activated); + QVERIFY(spy.isValid()); + + QLineEdit *lineEdit = new QLineEdit(QStringLiteral("Test"), &widget); + lineEdit->setReadOnly(true); + lineEdit->setFocus(); + + widget.show(); + + QVERIFY(QTest::qWaitForWindowActive(&widget)); + + const int keySequenceCount = keySequence.count(); + for (int i = 0; i < keySequenceCount; ++i) { + const uint key = keySequence[i]; + QTest::keyClick(lineEdit, + Qt::Key(key & ~Qt::KeyboardModifierMask), + Qt::KeyboardModifier(key & Qt::KeyboardModifierMask)); + } + + const int activationCount = shouldBeHandledByQLineEdit ? 0 : 1; + QCOMPARE(spy.count(), activationCount); +} + QTEST_MAIN(tst_QLineEdit) #include "tst_qlineedit.moc" -- cgit v1.2.3