From e24f75af4db89ebdfd3d48e9d007238a1b990cd2 Mon Sep 17 00:00:00 2001 From: Sune Vuorela Date: Mon, 16 Sep 2013 23:54:21 +0200 Subject: Implement QMainWindow::takeCentralWidget() This allows the application developer to restructure the application, including moving the central widget some place else. Change-Id: Idca2f74c190500db24404e020b0eb400e41aad10 Reviewed-by: Giuseppe D'Angelo --- src/widgets/widgets/qmainwindow.cpp | 16 +++++++ src/widgets/widgets/qmainwindow.h | 2 + .../widgets/qmainwindow/tst_qmainwindow.cpp | 55 ++++++++++++++++++++++ 3 files changed, 73 insertions(+) diff --git a/src/widgets/widgets/qmainwindow.cpp b/src/widgets/widgets/qmainwindow.cpp index 436fb65dd2..0df83189ba 100644 --- a/src/widgets/widgets/qmainwindow.cpp +++ b/src/widgets/widgets/qmainwindow.cpp @@ -629,6 +629,22 @@ void QMainWindow::setCentralWidget(QWidget *widget) d->layout->setCentralWidget(widget); } +/*! + Removes the central widget from this main window. + + The ownership of the removed widget is passed to the caller. + + \since Qt 5.2 +*/ +QWidget *QMainWindow::takeCentralWidget() +{ + Q_D(QMainWindow); + QWidget *oldcentralwidget = d->layout->centralWidget(); + oldcentralwidget->setParent(0); + d->layout->setCentralWidget(0); + return oldcentralwidget; +} + #ifndef QT_NO_DOCKWIDGET /*! Sets the given dock widget \a area to occupy the specified \a diff --git a/src/widgets/widgets/qmainwindow.h b/src/widgets/widgets/qmainwindow.h index d9edf711e0..8411bf9f6b 100644 --- a/src/widgets/widgets/qmainwindow.h +++ b/src/widgets/widgets/qmainwindow.h @@ -137,6 +137,8 @@ public: QWidget *centralWidget() const; void setCentralWidget(QWidget *widget); + QWidget *takeCentralWidget(); + #ifndef QT_NO_DOCKWIDGET void setCorner(Qt::Corner corner, Qt::DockWidgetArea area); Qt::DockWidgetArea corner(Qt::Corner corner) const; diff --git a/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp b/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp index b42bfcac81..e5a9b570bb 100644 --- a/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp +++ b/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp @@ -118,6 +118,7 @@ private slots: void toolButtonStyle(); void menuBar(); void centralWidget(); + void takeCentralWidget(); void corner(); void addToolBarBreak(); void insertToolBarBreak(); @@ -189,6 +190,14 @@ void tst_QMainWindow::getSetCheck() obj1.setCentralWidget((QWidget *)0); QCOMPARE((QWidget *)0, obj1.centralWidget()); // delete var3; // No delete, since QMainWindow takes ownership + + QWidget *var4 = new QWidget; + QPointer oldcentralwidget(var4); + obj1.setCentralWidget(var4); + obj1.setCentralWidget(new QWidget); + QCoreApplication::sendPostedEvents(var4, QEvent::DeferredDelete); + QVERIFY(oldcentralwidget.isNull()); + QVERIFY(obj1.centralWidget()->parent()); } tst_QMainWindow::tst_QMainWindow() @@ -806,6 +815,52 @@ void tst_QMainWindow::centralWidget() QVERIFY(w1 == 0); QVERIFY(w2 == 0); } + +} + +void tst_QMainWindow::takeCentralWidget() { + // test if takeCentralWidget works + QMainWindow mw; + + QPointer w1 = new QWidget; + + QVERIFY(mw.centralWidget() == 0); + + mw.setCentralWidget(w1); + + QWidget *oldCentralWidget = mw.takeCentralWidget(); + QVERIFY(oldCentralWidget == w1.data()); + + // ensure that takeCentralWidget doesn't end up calling deleteLater + // on the central widget + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + QVERIFY(mw.centralWidget() == 0); + QVERIFY(!w1.isNull()); + QVERIFY(w1->parent() == 0); + + mw.setCentralWidget(w1); + // ensure that the deleteLater called by setCentralWidget + // gets executed + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + QVERIFY(mw.centralWidget() == w1.data()); + + QPointer w2 = new QWidget; + + mw.setCentralWidget(w2); + // ensure w2 gets deleted + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + QVERIFY(w1.isNull()); + + QVERIFY(mw.centralWidget() == w2.data()); + + QWidget *hopefullyW2 = mw.takeCentralWidget(); + QVERIFY(mw.centralWidget() == 0); + // ensure that takeCentralWidget doesn't end up calling deleteLater + // on the central widget + QCoreApplication::sendPostedEvents(0, QEvent::DeferredDelete); + + QVERIFY(!w2.isNull()); + QCOMPARE(w2.data(), hopefullyW2); } void tst_QMainWindow::corner() -- cgit v1.2.3