diff options
author | Miikka Heikkinen <miikka.heikkinen@digia.com> | 2013-04-30 10:35:06 +0300 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-05-08 06:47:29 +0200 |
commit | 01bc34088eb8e7fb4842fa1c2117cddbac086136 (patch) | |
tree | 60153a770c9e50faa04afd3cb23073f1f5f5d776 | |
parent | bf2a0c795ed3a9c14e6cbbabc4456f3e9ebf7d6d (diff) |
Fix tabFocusFirst when that item is removed from QGraphicsScene
If tabFocusFirst is not cleared or set to another valid item,
there will be crash later if the removed item is deleted after
removal.
Task-number: QTBUG-30923
Change-Id: Iba9a6ce9334c52f8e552b0accda95ebb5fcfcdb1
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
-rw-r--r-- | src/widgets/graphicsview/qgraphicsscene.cpp | 9 | ||||
-rw-r--r-- | tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp | 18 |
2 files changed, 27 insertions, 0 deletions
diff --git a/src/widgets/graphicsview/qgraphicsscene.cpp b/src/widgets/graphicsview/qgraphicsscene.cpp index 8a0b983c73..88cccb5118 100644 --- a/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/src/widgets/graphicsview/qgraphicsscene.cpp @@ -637,6 +637,15 @@ void QGraphicsScenePrivate::removeItemHelper(QGraphicsItem *item) if (item == lastActivePanel) lastActivePanel = 0; + // Change tabFocusFirst to the next widget in focus chain if removing the current one. + if (item == tabFocusFirst) { + QGraphicsWidgetPrivate *wd = tabFocusFirst->d_func(); + if (wd->focusNext && wd->focusNext != tabFocusFirst && wd->focusNext->scene() == q) + tabFocusFirst = wd->focusNext; + else + tabFocusFirst = 0; + } + // Cancel active touches { QMap<int, QGraphicsItem *>::iterator it = itemForTouchPointId.begin(); diff --git a/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp b/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp index 08a10aa593..2bb588afed 100644 --- a/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicswidget/tst_qgraphicswidget.cpp @@ -1696,6 +1696,24 @@ void tst_QGraphicsWidget::verifyFocusChain() delete w; } { + // QTBUG-30923: + // Child QGraphicsWidget with tabFocusFirst gets removed & deleted should not crash. + // This case simulates what QtQuick1 does when you have QGraphicsObject based + // item in different qml views that are loaded one after another. + QGraphicsItem *parent1 = new QGraphicsRectItem(0); // root item + scene.addItem(parent1); + for (int i = 0; i < 2; i++) { + SubQGraphicsWidget *w1 = new SubQGraphicsWidget(parent1); + w1->setFocusPolicy(Qt::StrongFocus); + w1->setFocus(); + QVERIFY(w1->hasFocus()); + scene.removeItem(w1); + delete w1; + QApplication::processEvents(); + } + delete parent1; + } + { // remove the tabFocusFirst widget from the scene. QWidget *window = new QWidget; QVBoxLayout *layout = new QVBoxLayout; |