summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2014-11-13 21:41:04 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2015-01-04 18:33:49 +0100
commit84a3dacf462a4170a7fbc71b012fbb2db2c34a19 (patch)
treec6534f200492cbc27aae9d3c53aa22cf115fc1e2
parentc61f8df4047a616ffcb5e775fa8e5981b13e193f (diff)
QItemDelegate: let QTextEdit and QPlainTextEdit receive tab keypresses
We already let enter/return key presses to reach text edits instead of closing the editor. Do the same for tab/backtabs. [ChangeLog][QtWidgets][Important behavior changes] QItemDelegate will now not close a QTextEdit/QPlainTextEdit editor when the tab key is pressed; instead, the key will reach the editor. Task-number: QTBUG-3305 Change-Id: Ife9e6fdc5678535c596d1068770b0963134d8d5a Reviewed-by: Friedemann Kleint <Friedemann.Kleint@theqtcompany.com> Reviewed-by: Thorbjørn Lindeijer <bjorn@lindeijer.nl> Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
-rw-r--r--src/widgets/itemviews/qabstractitemdelegate.cpp33
-rw-r--r--tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp72
-rw-r--r--tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp19
3 files changed, 114 insertions, 10 deletions
diff --git a/src/widgets/itemviews/qabstractitemdelegate.cpp b/src/widgets/itemviews/qabstractitemdelegate.cpp
index 4d0840c3d6..c4c3d2acfd 100644
--- a/src/widgets/itemviews/qabstractitemdelegate.cpp
+++ b/src/widgets/itemviews/qabstractitemdelegate.cpp
@@ -420,6 +420,29 @@ QAbstractItemDelegatePrivate::QAbstractItemDelegatePrivate()
{
}
+static bool editorHandlesKeyEvent(QWidget *editor, const QKeyEvent *event)
+{
+#ifndef QT_NO_TEXTEDIT
+ // do not filter enter / return / tab / backtab for QTextEdit or QPlainTextEdit
+ if (qobject_cast<QTextEdit *>(editor) || qobject_cast<QPlainTextEdit *>(editor)) {
+ switch (event->key()) {
+ case Qt::Key_Tab:
+ case Qt::Key_Backtab:
+ case Qt::Key_Enter:
+ case Qt::Key_Return:
+ return true;
+
+ default:
+ break;
+ }
+ }
+#endif // QT_NO_TEXTEDIT
+
+ Q_UNUSED(editor);
+ Q_UNUSED(event);
+ return false;
+}
+
bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *event)
{
Q_Q(QAbstractItemDelegate);
@@ -428,7 +451,11 @@ bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *ev
if (!editor)
return false;
if (event->type() == QEvent::KeyPress) {
- switch (static_cast<QKeyEvent *>(event)->key()) {
+ QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
+ if (editorHandlesKeyEvent(editor, keyEvent))
+ return false;
+
+ switch (keyEvent->key()) {
case Qt::Key_Tab:
if (tryFixup(editor)) {
emit q->commitData(editor);
@@ -443,10 +470,6 @@ bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *ev
return true;
case Qt::Key_Enter:
case Qt::Key_Return:
-#ifndef QT_NO_TEXTEDIT
- if (qobject_cast<QTextEdit *>(editor) || qobject_cast<QPlainTextEdit *>(editor))
- return false; // don't filter enter key events for QTextEdit or QPlainTextEdit
-#endif // QT_NO_TEXTEDIT
// We want the editor to be able to process the key press
// before committing the data (e.g. so it can do
// validation/fixup of the input).
diff --git a/tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp b/tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp
index 1616b08fb2..4e2dbd4de4 100644
--- a/tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp
+++ b/tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp
@@ -34,13 +34,18 @@
#include <QStandardItemModel>
#include <QLineEdit>
#include <QComboBox>
+#include <QTextEdit>
+#include <QVBoxLayout>
#include <QTest>
#include <QSignalSpy>
+#include <QMetaType>
class tst_QDataWidgetMapper: public QObject
{
Q_OBJECT
private slots:
+ void initTestCase();
+
void setModel();
void navigate();
void addMapping();
@@ -50,8 +55,12 @@ private slots:
void mappedWidgetAt();
void comboBox();
+
+ void textEditDoesntChangeFocusOnTab_qtbug3305();
};
+Q_DECLARE_METATYPE(QAbstractItemDelegate::EndEditHint)
+
static QStandardItemModel *testModel(QObject *parent = 0)
{
QStandardItemModel *model = new QStandardItemModel(10, 10, parent);
@@ -64,6 +73,11 @@ static QStandardItemModel *testModel(QObject *parent = 0)
return model;
}
+void tst_QDataWidgetMapper::initTestCase()
+{
+ qRegisterMetaType<QAbstractItemDelegate::EndEditHint>();
+}
+
void tst_QDataWidgetMapper::setModel()
{
QDataWidgetMapper mapper;
@@ -403,5 +417,63 @@ void tst_QDataWidgetMapper::mappedWidgetAt()
QCOMPARE(mapper.mappedWidgetAt(4242), static_cast<QWidget *>(&lineEdit2));
}
+void tst_QDataWidgetMapper::textEditDoesntChangeFocusOnTab_qtbug3305()
+{
+ QDataWidgetMapper mapper;
+ QAbstractItemModel *model = testModel(&mapper);
+ mapper.setModel(model);
+
+ QSignalSpy closeEditorSpy(mapper.itemDelegate(), SIGNAL(closeEditor(QWidget*,QAbstractItemDelegate::EndEditHint)));
+ QVERIFY(closeEditorSpy.isValid());
+
+ QWidget container;
+ container.setLayout(new QVBoxLayout);
+
+ QLineEdit *lineEdit = new QLineEdit;
+ mapper.addMapping(lineEdit, 0);
+ container.layout()->addWidget(lineEdit);
+
+ QTextEdit *textEdit = new QTextEdit;
+ mapper.addMapping(textEdit, 1);
+ container.layout()->addWidget(textEdit);
+
+ lineEdit->setFocus();
+
+ container.show();
+
+ QApplication::setActiveWindow(&container);
+ QVERIFY(QTest::qWaitForWindowActive(&container));
+
+ int closeEditorSpyCount = 0;
+ const QString textEditContents = textEdit->toPlainText();
+
+ QCOMPARE(closeEditorSpy.count(), closeEditorSpyCount);
+ QVERIFY(lineEdit->hasFocus());
+ QVERIFY(!textEdit->hasFocus());
+
+ // this will generate a closeEditor for the tab key, and another for the focus out
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Tab);
+ closeEditorSpyCount += 2;
+ QTRY_COMPARE(closeEditorSpy.count(), closeEditorSpyCount);
+
+ QTRY_VERIFY(textEdit->hasFocus());
+ QVERIFY(!lineEdit->hasFocus());
+
+ // now that the text edit is focused, a tab keypress will insert a tab, not change focus
+ QTest::keyClick(QApplication::focusWidget(), Qt::Key_Tab);
+ QTRY_COMPARE(closeEditorSpy.count(), closeEditorSpyCount);
+
+ QVERIFY(!lineEdit->hasFocus());
+ QVERIFY(textEdit->hasFocus());
+ QCOMPARE(textEdit->toPlainText(), QLatin1Char('\t') + textEditContents);
+
+ // now give focus back to the line edit and check closeEditor gets emitted
+ lineEdit->setFocus();
+ QTRY_VERIFY(lineEdit->hasFocus());
+ QVERIFY(!textEdit->hasFocus());
+ ++closeEditorSpyCount;
+ QCOMPARE(closeEditorSpy.count(), closeEditorSpyCount);
+}
+
QTEST_MAIN(tst_QDataWidgetMapper)
#include "tst_qdatawidgetmapper.moc"
diff --git a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
index f035094a37..41fa889e83 100644
--- a/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
+++ b/tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp
@@ -1250,11 +1250,19 @@ void tst_QItemDelegate::enterKey_data()
QTest::addColumn<bool>("expectedFocus");
QTest::newRow("lineedit enter") << LineEdit << int(Qt::Key_Enter) << false;
+ QTest::newRow("lineedit return") << LineEdit << int(Qt::Key_Return) << false;
+ QTest::newRow("lineedit tab") << LineEdit << int(Qt::Key_Tab) << false;
+ QTest::newRow("lineedit backtab") << LineEdit << int(Qt::Key_Backtab) << false;
+
QTest::newRow("textedit enter") << TextEdit << int(Qt::Key_Enter) << true;
+ QTest::newRow("textedit return") << TextEdit << int(Qt::Key_Return) << true;
+ QTest::newRow("textedit tab") << TextEdit << int(Qt::Key_Tab) << true;
+ QTest::newRow("textedit backtab") << TextEdit << int(Qt::Key_Backtab) << false;
+
QTest::newRow("plaintextedit enter") << PlainTextEdit << int(Qt::Key_Enter) << true;
QTest::newRow("plaintextedit return") << PlainTextEdit << int(Qt::Key_Return) << true;
- QTest::newRow("plaintextedit tab") << PlainTextEdit << int(Qt::Key_Tab) << false;
- QTest::newRow("lineedit tab") << LineEdit << int(Qt::Key_Tab) << false;
+ QTest::newRow("plaintextedit tab") << PlainTextEdit << int(Qt::Key_Tab) << true;
+ QTest::newRow("plaintextedit backtab") << PlainTextEdit << int(Qt::Key_Backtab) << false;
}
void tst_QItemDelegate::enterKey()
@@ -1312,10 +1320,11 @@ void tst_QItemDelegate::enterKey()
QTest::keyClick(editor, Qt::Key(key));
QApplication::processEvents();
- // The line edit has already been destroyed, so avoid that case.
- if (widget == TextEdit || widget == PlainTextEdit) {
+ if (expectedFocus) {
QVERIFY(!editor.isNull());
- QCOMPARE(editor && editor->hasFocus(), expectedFocus);
+ QVERIFY(editor->hasFocus());
+ } else {
+ QTRY_VERIFY(editor.isNull()); // editor deletion happens via deleteLater
}
}