diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2014-11-13 21:41:04 +0100 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2015-01-04 18:33:49 +0100 |
commit | 84a3dacf462a4170a7fbc71b012fbb2db2c34a19 (patch) | |
tree | c6534f200492cbc27aae9d3c53aa22cf115fc1e2 /tests | |
parent | c61f8df4047a616ffcb5e775fa8e5981b13e193f (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>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/widgets/itemviews/qdatawidgetmapper/tst_qdatawidgetmapper.cpp | 72 | ||||
-rw-r--r-- | tests/auto/widgets/itemviews/qitemdelegate/tst_qitemdelegate.cpp | 19 |
2 files changed, 86 insertions, 5 deletions
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 } } |