From 0aa84a619ea0a7c85a1ed48ed28817d4c7e40b33 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Tue, 5 Aug 2014 11:35:20 +0200 Subject: [PATCH] QLineEdit: Disable standard key 'cut' when there is no selection. Task-number: QTBUG-40477 Change-Id: I0741a1a769c9e7e0d19e8aec231acc29461d44ea Reviewed-by: Marc Mutz --- src/widgets/widgets/qwidgetlinecontrol.cpp | 2 +- .../widgets/qlineedit/tst_qlineedit.cpp | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/widgets/widgets/qwidgetlinecontrol.cpp b/src/widgets/widgets/qwidgetlinecontrol.cpp index 2743e4cbbf..569308f5c8 100644 --- a/src/widgets/widgets/qwidgetlinecontrol.cpp +++ b/src/widgets/widgets/qwidgetlinecontrol.cpp @@ -1678,7 +1678,7 @@ void QWidgetLineControl::processKeyEvent(QKeyEvent* event) } } else if (event == QKeySequence::Cut) { - if (!isReadOnly()) { + if (!isReadOnly() && hasSelectedText()) { copy(); del(); } diff --git a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp index 0094a1112b..c7fba1c871 100644 --- a/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/widgets/widgets/qlineedit/tst_qlineedit.cpp @@ -244,6 +244,7 @@ private slots: #ifndef QT_NO_CLIPBOARD void cut(); + void cutWithoutSelection(); #endif void maxLengthAndInputMask(); void returnPressedKeyEvent(); @@ -2977,7 +2978,37 @@ void tst_QLineEdit::cut() testWidget->cut(); QCOMPARE(testWidget->text(), QString("Abcdefg defg hijklmno")); } -#endif + +void tst_QLineEdit::cutWithoutSelection() +{ + enum { selectionLength = 1 }; + + if (QKeySequence(QKeySequence::Cut).toString() != QLatin1String("Ctrl+X")) + QSKIP("Platform with non-standard keybindings"); + QClipboard *clipboard = QGuiApplication::clipboard(); + if (!PlatformClipboard::isAvailable() + || !QGuiApplication::platformName().compare("xcb", Qt::CaseInsensitive)) { // Avoid unstable X11 clipboard + clipboard = Q_NULLPTR; + } + + if (clipboard) + clipboard->clear(); + const QString origText = QStringLiteral("test"); + QLineEdit lineEdit(origText); + lineEdit.setCursorPosition(0); + QVERIFY(!lineEdit.hasSelectedText()); + QTest::keyClick(&lineEdit, Qt::Key_X, Qt::ControlModifier); + QCOMPARE(lineEdit.text(), origText); // No selection, unmodified. + if (clipboard) + QVERIFY(clipboard->text().isEmpty()); + lineEdit.setSelection(0, selectionLength); + QTest::keyClick(&lineEdit, Qt::Key_X, Qt::ControlModifier); + QCOMPARE(lineEdit.text(), origText.right(origText.size() - selectionLength)); + if (clipboard) + QCOMPARE(clipboard->text(), origText.left(selectionLength)); +} + +#endif // !QT_NO_CLIPBOARD class InputMaskValidator : public QValidator {