QMainWindow: fix restoreState() for toolbars

Restoring the toolbar state of a QMainWindow could not update
QWidgetPrivate::widgetItem for the QToolBar, because at that point it
was still holding the pointer to the widgetItem of the previous state.
Later on, when the new state was successfully applied, the previous
state was deleted, and the corresponding widgetItem was reset to
nullptr.

This patch explicitly resets the QToolBar's widgetItem while updating
the state, so that it is later correctly updated while creating a
new QWidgetItemV2.

Fixes: QTBUG-102395
Pick-to: 6.4 6.3 6.2 5.15
Change-Id: I17613d62423edcc0faf85ecb0a714865a50d87e8
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
bb10
Ivan Solovev 2022-06-16 13:16:58 +02:00
parent b2fb14b7ba
commit 2ed54dedf3
2 changed files with 9 additions and 0 deletions

View File

@ -1328,6 +1328,10 @@ bool QToolBarAreaLayout::restoreState(QDataStream &stream, const QList<QToolBar*
}
if (applyingLayout) {
// Clear the previous widgetItem for the toolBar, so that it's
// assigned correctly in QWidgetItemV2 constructor.
auto *toolBarPrivate = QWidgetPrivate::get(toolBar);
toolBarPrivate->widgetItem = nullptr;
item.widgetItem = new QWidgetItemV2(toolBar);
toolBar->setOrientation(floating ? ((shown & 2) ? Qt::Vertical : Qt::Horizontal) : dock.o);
toolBar->setVisible(shown & 1);

View File

@ -1340,14 +1340,19 @@ void tst_QMainWindow::restoreState()
dw.setObjectName(QLatin1String("dock"));
mw.addDockWidget(Qt::LeftDockWidgetArea, &dw);
QWidgetPrivate *tbp = QWidgetPrivate::get(&tb);
QVERIFY(tbp->widgetItem);
QByteArray state;
state = mw.saveState();
QVERIFY(mw.restoreState(state));
QVERIFY(tbp->widgetItem);
state = mw.saveState(1);
QVERIFY(!mw.restoreState(state));
QVERIFY(mw.restoreState(state, 1));
QVERIFY(tbp->widgetItem);
}
//tests the restoration of the previous versions of window settings