diff options
author | Daniel d'Andrada <daniel.dandrada@nokia.com> | 2010-11-09 14:48:52 +0200 |
---|---|---|
committer | Stanislav Ionascu <stanislav.ionascu@nokia.com> | 2010-11-09 15:26:56 +0200 |
commit | 860230c1a960b03e18a77228d2fd0de48f542bc8 (patch) | |
tree | 2d996dd0290226a91b054c5a91443dc90179e876 | |
parent | 0fc7c4bc3f80cd80c081e0440419a33d21d336bc (diff) |
Fixes: NB#201968 - MContainer should either take ownership of the centralWidget or not.
RevBy: Stanislav Ionascu
Details: The MContainer shall takes ownership by default of the central widget.
Merged with updated documentation for the MContainer::setCentralWidget.
-rw-r--r-- | src/corelib/widgets/mcontainer.cpp | 24 | ||||
-rw-r--r-- | src/corelib/widgets/mcontainer.h | 5 | ||||
-rw-r--r-- | tests/ut_mcontainer/ut_mcontainer.cpp | 37 | ||||
-rw-r--r-- | tests/ut_mcontainer/ut_mcontainer.h | 2 |
4 files changed, 58 insertions, 10 deletions
diff --git a/src/corelib/widgets/mcontainer.cpp b/src/corelib/widgets/mcontainer.cpp index 334e8dc2..d43e65fe 100644 --- a/src/corelib/widgets/mcontainer.cpp +++ b/src/corelib/widgets/mcontainer.cpp @@ -72,16 +72,20 @@ QGraphicsWidget *MContainer::centralWidget() void MContainer::setCentralWidget(QGraphicsWidget *centralWidget, bool destroy) { - if (centralWidget != NULL) { - const QGraphicsWidget *oldCentralWidget = model()->centralWidget(); - - // Set the new central widget - model()->setCentralWidget(centralWidget); - - // Destroy the old central widget if requested - if (destroy) { - delete oldCentralWidget; - } + QGraphicsWidget *oldCentralWidget = model()->centralWidget(); + + // Set the new central widget + model()->setCentralWidget(centralWidget); + + // Destroy the old central widget if requested + if (destroy) { + delete oldCentralWidget; + } else { + // pass the ownership back to the caller. + oldCentralWidget->setParentItem(0); + oldCentralWidget->setParent(0); + if (scene()) + scene()->removeItem(oldCentralWidget); } } diff --git a/src/corelib/widgets/mcontainer.h b/src/corelib/widgets/mcontainer.h index 9b891e96..f1e35231 100644 --- a/src/corelib/widgets/mcontainer.h +++ b/src/corelib/widgets/mcontainer.h @@ -133,6 +133,11 @@ public: /*! * \brief Sets the central widget for container. + * By default MContainer takes ownership of the centralWidget. + * + * When centralWidget is replaced and destroy parameter is passed as false, the old + * centralWidget is removed from graphics hierarchy and scene, and it's ownership of + * is passed to caller. */ void setCentralWidget(QGraphicsWidget *centralWidget, bool destroy = true); diff --git a/tests/ut_mcontainer/ut_mcontainer.cpp b/tests/ut_mcontainer/ut_mcontainer.cpp index 48e50d62..29ecceb9 100644 --- a/tests/ut_mcontainer/ut_mcontainer.cpp +++ b/tests/ut_mcontainer/ut_mcontainer.cpp @@ -21,6 +21,7 @@ #include <QGraphicsLinearLayout> #include <QGraphicsSceneMouseEvent> #include <QSignalSpy> +#include <QWeakPointer> #include <mcontainer.h> #include "mcontainer_p.h" @@ -72,6 +73,42 @@ void Ut_MContainer::setCentralWidget() QVERIFY(m_subject->centralWidget() == tmp); } +void Ut_MContainer::replaceCentralWidget() +{ + QGraphicsWidget *widget = new QGraphicsWidget; + QWeakPointer<QGraphicsWidget> widgetPointer(widget); + + m_subject->setCentralWidget(widget); + + QVERIFY(m_subject->centralWidget() == widget); + + m_subject->setCentralWidget(0); + + QVERIFY(m_subject->centralWidget() == 0); + QCOMPARE(widgetPointer.isNull(), true); +} + +void Ut_MContainer::replaceCentralWidgetWithoutDestroying() +{ + QGraphicsWidget *widget = new QGraphicsWidget; + QWeakPointer<QGraphicsWidget> widgetPointer(widget); + + m_subject->setCentralWidget(widget); + + QVERIFY(m_subject->centralWidget() == widget); + + m_subject->setCentralWidget(0, false); + + QVERIFY(m_subject->centralWidget() == 0); + QCOMPARE(widgetPointer.isNull(), false); + QVERIFY(widget->parentItem() == 0); + QVERIFY(widget->parent() == 0); + QVERIFY(widget->scene() == 0); + + // clean up + delete widget; +} + void Ut_MContainer::title() { QString myQString("testing title()"); diff --git a/tests/ut_mcontainer/ut_mcontainer.h b/tests/ut_mcontainer/ut_mcontainer.h index 98df3716..888f0d26 100644 --- a/tests/ut_mcontainer/ut_mcontainer.h +++ b/tests/ut_mcontainer/ut_mcontainer.h @@ -39,6 +39,8 @@ private slots: void centralWidget(); void setCentralWidget(); + void replaceCentralWidget(); + void replaceCentralWidgetWithoutDestroying(); void title(); void text(); |