QLineEdit: Disable standard key 'cut' when there is no selection.

Task-number: QTBUG-40477
Change-Id: I0741a1a769c9e7e0d19e8aec231acc29461d44ea
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Friedemann Kleint 2014-08-05 11:35:20 +02:00
parent 9b599c6cea
commit 0aa84a619e
2 changed files with 33 additions and 2 deletions

View File

@ -1678,7 +1678,7 @@ void QWidgetLineControl::processKeyEvent(QKeyEvent* event)
}
}
else if (event == QKeySequence::Cut) {
if (!isReadOnly()) {
if (!isReadOnly() && hasSelectedText()) {
copy();
del();
}

View File

@ -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
{