summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/gui/graphicsview/qgraphicsscene.cpp7
-rw-r--r--tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp58
2 files changed, 63 insertions, 2 deletions
diff --git a/src/gui/graphicsview/qgraphicsscene.cpp b/src/gui/graphicsview/qgraphicsscene.cpp
index fbd78d9..97bc010 100644
--- a/src/gui/graphicsview/qgraphicsscene.cpp
+++ b/src/gui/graphicsview/qgraphicsscene.cpp
@@ -581,6 +581,9 @@ void QGraphicsScenePrivate::setActivePanelHelper(QGraphicsItem *item, bool durin
return;
}
+ // Ensure the scene has focus when we change panel activation.
+ q->setFocus(Qt::ActiveWindowFocusReason);
+
// Find the item's panel.
QGraphicsItem *panel = item ? item->panel() : 0;
lastActivePanel = panel ? activePanel : 0;
@@ -2431,7 +2434,7 @@ void QGraphicsScene::addItem(QGraphicsItem *item)
// Ensure that newly added items that have subfocus set, gain
// focus automatically if there isn't a focus item already.
- if (!d->focusItem && item->focusItem() && item->isActive())
+ if (!d->focusItem && item->focusItem())
item->focusItem()->setFocus();
d->updateInputMethodSensitivityInViews();
@@ -2780,7 +2783,7 @@ bool QGraphicsScene::hasFocus() const
void QGraphicsScene::setFocus(Qt::FocusReason focusReason)
{
Q_D(QGraphicsScene);
- if (d->hasFocus)
+ if (d->hasFocus || !isActive())
return;
QFocusEvent event(QEvent::FocusIn, focusReason);
QCoreApplication::sendEvent(this, &event);
diff --git a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp
index 6998684..07d7cce 100644
--- a/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp
+++ b/tests/auto/qgraphicsscene/tst_qgraphicsscene.cpp
@@ -264,6 +264,8 @@ private slots:
void inputMethod_data();
void inputMethod();
void dispatchHoverOnPress();
+ void initialFocus_data();
+ void initialFocus();
// task specific tests below me
void task139710_bspTreeCrash();
@@ -3822,5 +3824,61 @@ void tst_QGraphicsScene::dispatchHoverOnPress()
}
}
+void tst_QGraphicsScene::initialFocus_data()
+{
+ QTest::addColumn<bool>("activeScene");
+ QTest::addColumn<bool>("explicitSetFocus");
+ QTest::addColumn<bool>("isPanel");
+ QTest::addColumn<bool>("shouldHaveFocus");
+
+ QTest::newRow("inactive scene, normal item") << false << false << false << false;
+ QTest::newRow("inactive scene, panel item") << false << false << true << false;
+ QTest::newRow("inactive scene, normal item, explicit focus") << false << true << false << true;
+ QTest::newRow("inactive scene, panel, explicit focus") << false << true << true << true;
+ QTest::newRow("active scene, normal item") << true << false << false << false;
+ QTest::newRow("active scene, panel item") << true << false << true << false;
+ QTest::newRow("active scene, normal item, explicit focus") << true << true << false << true;
+ QTest::newRow("active scene, panel, explicit focus") << true << true << true << true;
+}
+
+void tst_QGraphicsScene::initialFocus()
+{
+ QFETCH(bool, activeScene);
+ QFETCH(bool, explicitSetFocus);
+ QFETCH(bool, isPanel);
+ QFETCH(bool, shouldHaveFocus);
+
+ QGraphicsRectItem *rect = new QGraphicsRectItem;
+ rect->setFlag(QGraphicsItem::ItemIsFocusable);
+ QVERIFY(!rect->hasFocus());
+
+ if (isPanel)
+ rect->setFlag(QGraphicsItem::ItemIsPanel);
+
+ // Setting focus on an item before adding to the scene will ensure
+ // it gets focus when the scene is activated.
+ if (explicitSetFocus)
+ rect->setFocus();
+
+ QGraphicsScene scene;
+ QVERIFY(!scene.isActive());
+
+ if (activeScene) {
+ QEvent windowActivate(QEvent::WindowActivate);
+ qApp->sendEvent(&scene, &windowActivate);
+ scene.setFocus();
+ }
+
+ scene.addItem(rect);
+
+ if (!activeScene) {
+ QEvent windowActivate(QEvent::WindowActivate);
+ qApp->sendEvent(&scene, &windowActivate);
+ scene.setFocus();
+ }
+
+ QCOMPARE(rect->hasFocus(), shouldHaveFocus);
+}
+
QTEST_MAIN(tst_QGraphicsScene)
#include "tst_qgraphicsscene.moc"