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 <giuseppe.dangelo@kdab.com>bb10
parent
938c838c10
commit
e24f75af4d
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Reference in New Issue