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
Change-Id: I17613d62423edcc0faf85ecb0a714865a50d87e8
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
(cherry picked from commit 2ed54dedf3783471716b21509db1f8279d5b5af8)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Ivan Solovev 2022-06-16 13:16:58 +02:00 committed by Qt Cherry-pick Bot
parent 8562f3aee6
commit 1ed89b7012
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) { 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); item.widgetItem = new QWidgetItemV2(toolBar);
toolBar->setOrientation(floating ? ((shown & 2) ? Qt::Vertical : Qt::Horizontal) : dock.o); toolBar->setOrientation(floating ? ((shown & 2) ? Qt::Vertical : Qt::Horizontal) : dock.o);
toolBar->setVisible(shown & 1); toolBar->setVisible(shown & 1);

View File

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