Make sure the parent view will have focus when activation is given back

When the focus is lost on an editor due to the application no longer
being the active one then we have to ensure the parent view is going to
get the focus when it is returned. Since the editor does not have focus
when this check is done we need to manually account for this case by
setting it on the parent view as if it would if the editor did have
focus.

Task-number: QTBUG-62253
Change-Id: I14ac347e9e3a2bfaa8715a45811b17c1c7cf15f8
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
This commit is contained in:
Andy Shaw 2017-08-24 09:30:14 +02:00
parent 8920bf32ee
commit d0dffdfc01
2 changed files with 87 additions and 0 deletions

View File

@ -526,7 +526,15 @@ bool QAbstractItemDelegatePrivate::editorEventFilter(QObject *object, QEvent *ev
if (tryFixup(editor))
emit q->commitData(editor);
// If the application loses focus while editing, then the focus needs to go back
// to the itemview when the editor closes. This ensures that when the application
// is active again it will have the focus on the itemview as expected.
const bool manuallyFixFocus = (event->type() == QEvent::FocusOut) && !editor->hasFocus() &&
editor->parentWidget() &&
(static_cast<QFocusEvent *>(event)->reason() == Qt::ActiveWindowFocusReason);
emit q->closeEditor(editor, QAbstractItemDelegate::NoHint);
if (manuallyFixFocus)
editor->parentWidget()->setFocus();
}
#ifndef QT_NO_SHORTCUT
} else if (event->type() == QEvent::ShortcutOverride) {

View File

@ -149,6 +149,8 @@ private slots:
void inputMethodEnabled();
void currentFollowsIndexWidget_data();
void currentFollowsIndexWidget();
void checkFocusAfterActivationChanges_data();
void checkFocusAfterActivationChanges();
};
class MyAbstractItemDelegate : public QAbstractItemDelegate
@ -2443,5 +2445,82 @@ void tst_QAbstractItemView::currentFollowsIndexWidget()
QCOMPARE(view->currentIndex(), item1->index());
}
class EditorItemDelegate : public QItemDelegate
{
public:
EditorItemDelegate() : QItemDelegate(), openedEditor(nullptr) { }
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &,
const QModelIndex &) const override
{
openedEditor = new QLineEdit(parent);
return openedEditor;
}
mutable QPointer<QWidget> openedEditor;
};
// Testing the case reported in QTBUG-62253.
// When an itemview with an editor that has focus loses focus
// due to a change in the active window then we need to check
// that the itemview gets focus once the activation is back
// on the original window.
void tst_QAbstractItemView::checkFocusAfterActivationChanges_data()
{
QTest::addColumn<QString>("viewType");
QTest::newRow("QListView") << "QListView";
QTest::newRow("QTableView") << "QTableView";
QTest::newRow("QTreeView") << "QTreeView";
}
void tst_QAbstractItemView::checkFocusAfterActivationChanges()
{
QFETCH(QString, viewType);
const QRect availableGeo = qApp->primaryScreen()->availableGeometry();
const int halfWidth = availableGeo.width() / 2;
QWidget otherTopLevel;
otherTopLevel.setGeometry(availableGeo.x(), availableGeo.y(),
halfWidth, availableGeo.height());
otherTopLevel.show();
QWidget w;
w.setGeometry(availableGeo.x() + halfWidth, availableGeo.y(),
halfWidth, availableGeo.height());
QLineEdit *le = new QLineEdit(&w);
QAbstractItemView *view = 0;
if (viewType == "QListView")
view = new QListView(&w);
else if (viewType == "QTableView")
view = new QTableView(&w);
else if (viewType == "QTreeView")
view = new QTreeView(&w);
QStandardItemModel model(5, 5);
view->setModel(&model);
view->move(0, 50);
EditorItemDelegate delegate;
view->setItemDelegate(&delegate);
w.show();
QTest::qWaitForWindowActive(&w);
QVERIFY(le->hasFocus());
view->setFocus();
QVERIFY(view->hasFocus());
view->edit(model.index(0,0));
QVERIFY(QTest::qWaitForWindowExposed(delegate.openedEditor));
QVERIFY(delegate.openedEditor->hasFocus());
QApplication::setActiveWindow(&otherTopLevel);
QTest::qWaitForWindowActive(&otherTopLevel);
otherTopLevel.setFocus();
QVERIFY(!delegate.openedEditor);
QApplication::setActiveWindow(&w);
QTest::qWaitForWindowActive(&w);
QVERIFY(view->hasFocus());
}
QTEST_MAIN(tst_QAbstractItemView)
#include "tst_qabstractitemview.moc"