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>
This commit is contained in:
Giuseppe D'Angelo 2014-11-13 21:41:04 +01:00
parent c61f8df404
commit 84a3dacf46
3 changed files with 114 additions and 10 deletions

View File

@ -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).

View File

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

View File

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