Item views: respect focus policy when closing an editor

Task-number: QTBUG-31411
Change-Id: Ib0a72755c35a553653ea014672d59979a550b7ae
Reviewed-by: Thorbjørn Lund Martsum <tmartsum@gmail.com>
bb10
J-P Nurmi 2014-03-28 15:03:01 +01:00 committed by The Qt Project
parent f4f23bd5b0
commit cc08fc7bfd
4 changed files with 63 additions and 11 deletions

View File

@ -2753,10 +2753,14 @@ void QAbstractItemView::closeEditor(QWidget *editor, QAbstractItemDelegate::EndE
editor->removeEventFilter(d->delegateForIndex(index));
d->removeEditor(editor);
}
if (hadFocus)
setFocus(); // this will send a focusLost event to the editor
else
if (hadFocus) {
if (focusPolicy() != Qt::NoFocus)
setFocus(); // this will send a focusLost event to the editor
else
editor->clearFocus();
} else {
d->checkPersistentEditorFocus();
}
QPointer<QWidget> ed = editor;
QApplication::sendPostedEvents(editor, 0);

View File

@ -1210,13 +1210,10 @@ bool QItemDelegate::eventFilter(QObject *object, QEvent *event)
case Qt::Key_Escape:
// don't commit data
emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
break;
return true;
default:
return false;
}
if (editor->parentWidget())
editor->parentWidget()->setFocus();
return true;
} else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
//the Hide event will take care of he editors that are in fact complete dialogs
if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {

View File

@ -678,13 +678,10 @@ bool QStyledItemDelegate::eventFilter(QObject *object, QEvent *event)
case Qt::Key_Escape:
// don't commit data
emit closeEditor(editor, QAbstractItemDelegate::RevertModelCache);
break;
return true;
default:
return false;
}
if (editor->parentWidget())
editor->parentWidget()->setFocus();
return true;
} else if (event->type() == QEvent::FocusOut || (event->type() == QEvent::Hide && editor->isWindow())) {
//the Hide event will take care of he editors that are in fact complete dialogs
if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {

View File

@ -60,6 +60,7 @@
#include <qlineedit.h>
#include <qscreen.h>
#include <qscopedpointer.h>
#include <qstyleditemdelegate.h>
static inline void setFrameless(QWidget *w)
{
@ -244,6 +245,8 @@ private slots:
void testChangeEditorState();
void deselectInSingleSelection();
void testNoActivateOnDisabledItem();
void testFocusPolicy_data();
void testFocusPolicy();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -1728,5 +1731,56 @@ void tst_QAbstractItemView::testNoActivateOnDisabledItem()
QCOMPARE(activatedSpy.count(), 0);
}
void tst_QAbstractItemView::testFocusPolicy_data()
{
QTest::addColumn<QAbstractItemDelegate*>("delegate");
QAbstractItemDelegate *styledItemDelegate = new QStyledItemDelegate(this);
QAbstractItemDelegate *itemDelegate = new QItemDelegate(this);
QTest::newRow("QStyledItemDelegate") << styledItemDelegate;
QTest::newRow("QItemDelegate") << itemDelegate;
}
void tst_QAbstractItemView::testFocusPolicy()
{
QFETCH(QAbstractItemDelegate*, delegate);
QWidget window;
QTableView *table = new QTableView(&window);
table->setItemDelegate(delegate);
QVBoxLayout *layout = new QVBoxLayout(&window);
layout->addWidget(table);
QStandardItemModel model;
model.setRowCount(10);
model.setColumnCount(10);
table->setModel(&model);
table->setCurrentIndex(model.index(1, 1));
centerOnScreen(&window);
moveCursorAway(&window);
window.show();
QApplication::setActiveWindow(&window);
QVERIFY(QTest::qWaitForWindowActive(&window));
// itemview accepts focus => editor is closed => return focus to the itemview
QPoint clickpos = table->visualRect(model.index(1, 1)).center();
QTest::mouseDClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos);
QWidget *editor = qApp->focusWidget();
QVERIFY(editor);
QTest::keyClick(editor, Qt::Key_Escape, Qt::NoModifier);
QCOMPARE(qApp->focusWidget(), table);
// itemview doesn't accept focus => editor is closed => clear the focus
table->setFocusPolicy(Qt::NoFocus);
QTest::mouseDClick(table->viewport(), Qt::LeftButton, Qt::NoModifier, clickpos);
editor = qApp->focusWidget();
QVERIFY(editor);
QTest::keyClick(editor, Qt::Key_Escape, Qt::NoModifier);
QVERIFY(!qApp->focusWidget());
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"