diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index 7033eeea22..86a830a9f9 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -1879,7 +1879,10 @@ void QWidgetLineControl::processKeyEvent(QKeyEvent* event) unknown = false; } - if (unknown && !isReadOnly()) { + // QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards + if (unknown && !isReadOnly() + && event->modifiers() != Qt::ControlModifier + && event->modifiers() != (Qt::ControlModifier | Qt::ShiftModifier)) { QString t = event->text(); if (!t.isEmpty() && t.at(0).isPrint()) { insert(t); diff --git a/src/widgets/widgets/qwidgettextcontrol.cpp b/src/widgets/widgets/qwidgettextcontrol.cpp index 8c48533a03..2e1e87be72 100644 --- a/src/widgets/widgets/qwidgettextcontrol.cpp +++ b/src/widgets/widgets/qwidgettextcontrol.cpp @@ -1339,6 +1339,12 @@ void QWidgetTextControlPrivate::keyPressEvent(QKeyEvent *e) process: { + // QTBUG-35734: ignore Ctrl/Ctrl+Shift; accept only AltGr (Alt+Ctrl) on German keyboards + if (e->modifiers() == Qt::ControlModifier + || e->modifiers() == (Qt::ShiftModifier | Qt::ControlModifier)) { + e->ignore(); + return; + } QString text = e->text(); if (!text.isEmpty() && (text.at(0).isPrint() || text.at(0) == QLatin1Char('\t'))) { if (overwriteMode diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index fec79326c8..5b467f4f17 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -249,6 +249,7 @@ private slots: void editInvalidText(); + void charWithAltOrCtrlModifier_data(); void charWithAltOrCtrlModifier(); void inlineCompletion(); @@ -3224,19 +3225,35 @@ void tst_QLineEdit::editInvalidText() testWidget->setValidator(0); } +Q_DECLARE_METATYPE(Qt::KeyboardModifiers) + +void tst_QLineEdit::charWithAltOrCtrlModifier_data() +{ + QTest::addColumn("modifiers"); + QTest::addColumn("textExpected"); + QTest::newRow("no-modifiers") << Qt::KeyboardModifiers() << true; + // Ctrl, Ctrl+Shift: No text (QTBUG-35734) + QTest::newRow("ctrl") << Qt::KeyboardModifiers(Qt::ControlModifier) + << false; + QTest::newRow("ctrl-shift") << Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::ControlModifier) + << false; + QTest::newRow("alt") << Qt::KeyboardModifiers(Qt::AltModifier) << true; + // Alt-Ctrl (Alt-Gr on German keyboards, Task 129098): Expect text + QTest::newRow("alt-ctrl") << (Qt::AltModifier | Qt::ControlModifier) << true; +} + void tst_QLineEdit::charWithAltOrCtrlModifier() { + QFETCH(Qt::KeyboardModifiers, modifiers); + QFETCH(bool, textExpected); + QLineEdit *testWidget = ensureTestWidget(); testWidget->clear(); - QCOMPARE(testWidget->text(), QString("")); - QTest::keyPress(testWidget, Qt::Key_Plus); - QCOMPARE(testWidget->text(), QString("+")); - QTest::keyPress(testWidget, Qt::Key_Plus, Qt::ControlModifier); - QCOMPARE(testWidget->text(), QString("++")); - QTest::keyPress(testWidget, Qt::Key_Plus, Qt::AltModifier); - QCOMPARE(testWidget->text(), QString("+++")); - QTest::keyPress(testWidget, Qt::Key_Plus, Qt::AltModifier | Qt::ControlModifier); - QCOMPARE(testWidget->text(), QString("++++")); + QVERIFY(testWidget->text().isEmpty()); + + QTest::keyPress(testWidget, Qt::Key_Plus, modifiers); + const QString expectedText = textExpected ? QLatin1String("+") : QString(); + QCOMPARE(testWidget->text(), expectedText); } void tst_QLineEdit::leftKeyOnSelectedText() diff --git a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp index cf495e2238..390b99c6e7 100644 --- a/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp +++ b/tests/auto/widgets/widgets/qplaintextedit/tst_qplaintextedit.cpp @@ -99,7 +99,8 @@ private slots: #ifndef QT_NO_CLIPBOARD void copyAndSelectAllInReadonly(); #endif - void ctrlAltInput(); + void charWithAltOrCtrlModifier_data(); + void charWithAltOrCtrlModifier(); void noPropertiesOnDefaultTextEditCharFormat(); void setPlainTextShouldEmitTextChangedOnce(); void overwriteMode(); @@ -691,10 +692,34 @@ void tst_QPlainTextEdit::copyAndSelectAllInReadonly() } #endif -void tst_QPlainTextEdit::ctrlAltInput() +Q_DECLARE_METATYPE(Qt::KeyboardModifiers) + +// Test how QWidgetTextControlPrivate (used in QPlainTextEdit, QTextEdit) +// handles input with modifiers. +void tst_QPlainTextEdit::charWithAltOrCtrlModifier_data() { - QTest::keyClick(ed, Qt::Key_At, Qt::ControlModifier | Qt::AltModifier); - QCOMPARE(ed->toPlainText(), QString("@")); + QTest::addColumn("modifiers"); + QTest::addColumn("textExpected"); + + QTest::newRow("no-modifiers") << Qt::KeyboardModifiers() << true; + // Ctrl, Ctrl+Shift: No text (QTBUG-35734) + QTest::newRow("ctrl") << Qt::KeyboardModifiers(Qt::ControlModifier) + << false; + QTest::newRow("ctrl-shift") << Qt::KeyboardModifiers(Qt::ShiftModifier | Qt::ControlModifier) + << false; + QTest::newRow("alt") << Qt::KeyboardModifiers(Qt::AltModifier) << true; + // Alt-Ctrl (Alt-Gr on German keyboards, Task 129098): Expect text + QTest::newRow("alt-ctrl") << (Qt::AltModifier | Qt::ControlModifier) << true; +} + +void tst_QPlainTextEdit::charWithAltOrCtrlModifier() +{ + QFETCH(Qt::KeyboardModifiers, modifiers); + QFETCH(bool, textExpected); + + QTest::keyClick(ed, Qt::Key_At, modifiers); + const QString expectedText = textExpected ? QLatin1String("@") : QString(); + QCOMPARE(ed->toPlainText(), expectedText); } void tst_QPlainTextEdit::noPropertiesOnDefaultTextEditCharFormat() diff --git a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp index 8a82ee433d..220185c04f 100644 --- a/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp +++ b/tests/auto/widgets/widgets/qtextedit/tst_qtextedit.cpp @@ -122,7 +122,6 @@ private slots: #ifndef QT_NO_CLIPBOARD void copyAndSelectAllInReadonly(); #endif - void ctrlAltInput(); void noPropertiesOnDefaultTextEditCharFormat(); void setPlainTextShouldUseCurrentCharFormat(); void setPlainTextShouldEmitTextChangedOnce(); @@ -1042,12 +1041,6 @@ void tst_QTextEdit::copyAndSelectAllInReadonly() } #endif -void tst_QTextEdit::ctrlAltInput() -{ - QTest::keyClick(ed, Qt::Key_At, Qt::ControlModifier | Qt::AltModifier); - QCOMPARE(ed->toPlainText(), QString("@")); -} - void tst_QTextEdit::noPropertiesOnDefaultTextEditCharFormat() { // there should be no properties set on the default/initial char format