diff options
author | Andy Shaw <andy.shaw@qt.io> | 2017-08-24 09:30:14 +0200 |
---|---|---|
committer | Andy Shaw <andy.shaw@qt.io> | 2018-02-15 07:45:26 +0000 |
commit | d0dffdfc012574da4a75241097b667d09bb39ba2 (patch) | |
tree | 95a1ce711ff0f7792301aa912fbe09ab3214049c /tests/auto/widgets/itemviews | |
parent | 8920bf32eebe03cfc8a1a5e97f5b34c09c79a11b (diff) |
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>
Diffstat (limited to 'tests/auto/widgets/itemviews')
-rw-r--r-- | tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp index e99ed8f2f4..4723221293 100644 --- a/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp +++ b/tests/auto/widgets/itemviews/qabstractitemview/tst_qabstractitemview.cpp @@ -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" |