summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/widgets/widgets/qmainwindow.cpp16
-rw-r--r--src/widgets/widgets/qmainwindow.h2
-rw-r--r--tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp55
3 files changed, 73 insertions, 0 deletions
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<QWidget> 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<QWidget> 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<QWidget> 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()