From 701bdcbc8b7751834f6d24844bcb91a23cbdc409 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Fri, 22 May 2009 15:48:23 +0200 Subject: Pressing enter in a QPlainTextEdit embedded on a itemview should insert a newline Do the same special case as for QTextEdit (yes, this is a pitty that we have special cases like that Reviewed-by: Thierry Task-number: 252532 --- src/gui/itemviews/qitemdelegate.cpp | 3 +- src/gui/itemviews/qstyleditemdelegate.cpp | 3 +- tests/auto/qitemdelegate/tst_qitemdelegate.cpp | 78 ++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) diff --git a/src/gui/itemviews/qitemdelegate.cpp b/src/gui/itemviews/qitemdelegate.cpp index 3b7eb2d..a748199 100644 --- a/src/gui/itemviews/qitemdelegate.cpp +++ b/src/gui/itemviews/qitemdelegate.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -1194,7 +1195,7 @@ bool QItemDelegate::eventFilter(QObject *object, QEvent *event) case Qt::Key_Enter: case Qt::Key_Return: #ifndef QT_NO_TEXTEDIT - if (qobject_cast(editor)) + if (qobject_cast(editor) || qobject_cast(editor)) return false; // don't filter enter key events for QTextEdit // We want the editor to be able to process the key press // before committing the data (e.g. so it can do diff --git a/src/gui/itemviews/qstyleditemdelegate.cpp b/src/gui/itemviews/qstyleditemdelegate.cpp index be0971b..7f2c8ed 100644 --- a/src/gui/itemviews/qstyleditemdelegate.cpp +++ b/src/gui/itemviews/qstyleditemdelegate.cpp @@ -47,6 +47,7 @@ #include #include #include +#include #include #include #include @@ -646,7 +647,7 @@ bool QStyledItemDelegate::eventFilter(QObject *object, QEvent *event) case Qt::Key_Enter: case Qt::Key_Return: #ifndef QT_NO_TEXTEDIT - if (qobject_cast(editor)) + if (qobject_cast(editor) || qobject_cast(editor)) return false; // don't filter enter key events for QTextEdit // We want the editor to be able to process the key press // before committing the data (e.g. so it can do diff --git a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp index 27741e0..615ac01 100644 --- a/tests/auto/qitemdelegate/tst_qitemdelegate.cpp +++ b/tests/auto/qitemdelegate/tst_qitemdelegate.cpp @@ -59,6 +59,8 @@ #include #include +#include +#include Q_DECLARE_METATYPE(QAbstractItemDelegate::EndEditHint) @@ -226,6 +228,8 @@ private slots: void decoration(); void editorEvent_data(); void editorEvent(); + void enterKey_data(); + void enterKey(); }; @@ -1048,6 +1052,80 @@ void tst_QItemDelegate::editorEvent() QCOMPARE(index.data(Qt::CheckStateRole).toInt(), expectedCheckState); } +void tst_QItemDelegate::enterKey_data() +{ + QTest::addColumn("widget"); + QTest::addColumn("key"); + QTest::addColumn("expectedFocus"); + + QTest::newRow("lineedit enter") << 1 << int(Qt::Key_Enter) << false; + QTest::newRow("textedit enter") << 2 << int(Qt::Key_Enter) << true; + QTest::newRow("plaintextedit enter") << 3 << int(Qt::Key_Enter) << true; + QTest::newRow("plaintextedit return") << 3 << int(Qt::Key_Return) << true; + QTest::newRow("plaintextedit tab") << 3 << int(Qt::Key_Tab) << false; + QTest::newRow("lineedit tab") << 1 << int(Qt::Key_Tab) << false; +} + +void tst_QItemDelegate::enterKey() +{ + QFETCH(int, widget); + QFETCH(int, key); + QFETCH(bool, expectedFocus); + + QStandardItemModel model; + model.appendRow(new QStandardItem()); + + QListView view; + view.setModel(&model); + view.show(); + QApplication::setActiveWindow(&view); + view.setFocus(); + QTest::qWait(30); + + struct TestDelegate : public QItemDelegate + { + int widgetType; + virtual QWidget* createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& /*index*/) const + { + QWidget *editor = 0; + switch(widgetType) { + case 1: + editor = new QLineEdit(parent); + break; + case 2: + editor = new QTextEdit(parent); + break; + case 3: + editor = new QPlainTextEdit(parent); + break; + } + editor->setObjectName(QString::fromLatin1("TheEditor")); + return editor; + } + } delegate; + + delegate.widgetType = widget; + + view.setItemDelegate(&delegate); + QModelIndex index = model.index(0, 0); + view.setCurrentIndex(index); // the editor will only selectAll on the current index + view.edit(index); + QTest::qWait(30); + + QList lineEditors = qFindChildren(view.viewport(), QString::fromLatin1("TheEditor")); + QCOMPARE(lineEditors.count(), 1); + + QWidget *editor = lineEditors.at(0); + QCOMPARE(editor->hasFocus(), true); + + QTest::keyClick(editor, Qt::Key(key)); + QApplication::processEvents(); + + QCOMPARE(editor->hasFocus(), expectedFocus); +} + + + // ### _not_ covered: // editing with a custom editor factory -- cgit v0.12