diff options
-rw-r--r-- | src/gui/widgets/qlinecontrol.cpp | 2 | ||||
-rw-r--r-- | tests/auto/qlineedit/tst_qlineedit.cpp | 29 |
2 files changed, 30 insertions, 1 deletions
diff --git a/src/gui/widgets/qlinecontrol.cpp b/src/gui/widgets/qlinecontrol.cpp index 8ff2aea..df8269f 100644 --- a/src/gui/widgets/qlinecontrol.cpp +++ b/src/gui/widgets/qlinecontrol.cpp @@ -1699,7 +1699,7 @@ void QLineControl::processKeyEvent(QKeyEvent* event) } } else if (event == QKeySequence::Cut) { - if (!isReadOnly()) { + if (!isReadOnly() && hasSelectedText()) { copy(); del(); } diff --git a/tests/auto/qlineedit/tst_qlineedit.cpp b/tests/auto/qlineedit/tst_qlineedit.cpp index 6431e34..22f39f2 100644 --- a/tests/auto/qlineedit/tst_qlineedit.cpp +++ b/tests/auto/qlineedit/tst_qlineedit.cpp @@ -236,6 +236,7 @@ private slots: #ifndef QT_NO_CLIPBOARD void cut(); + void cutWithoutSelection(); void copy(); void paste(); #endif @@ -3013,6 +3014,34 @@ void tst_QLineEdit::cut() QCOMPARE(testWidget->text(), QString("Abcdefg defg hijklmno")); } +void tst_QLineEdit::cutWithoutSelection() +{ + enum { selectionLength = 1 }; + + if (QKeySequence(QKeySequence::Cut).toString() != QLatin1String("Ctrl+X")) + QSKIP("Platform with non-standard keybindings", SkipAll); + QClipboard *clipboard = QApplication::clipboard(); +#ifdef Q_WS_X11 + clipboard = 0; // Avoid unstable X11 clipboard +#endif + + if (clipboard) + clipboard->clear(); + const QString origText("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)); +} + void tst_QLineEdit::copy() { DEPENDS_ON("cut"); |