From 1ed89b70122f7a9ba9f315bbebe3c57773bc9705 Mon Sep 17 00:00:00 2001 From: Ivan Solovev Date: Thu, 16 Jun 2022 13:16:58 +0200 Subject: [PATCH] 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 (cherry picked from commit 2ed54dedf3783471716b21509db1f8279d5b5af8) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qtoolbararealayout.cpp | 4 ++++ tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/widgets/widgets/qtoolbararealayout.cpp b/src/widgets/widgets/qtoolbararealayout.cpp index 09929e72341..adddfbc5297 100644 --- a/src/widgets/widgets/qtoolbararealayout.cpp +++ b/src/widgets/widgets/qtoolbararealayout.cpp @@ -1328,6 +1328,10 @@ bool QToolBarAreaLayout::restoreState(QDataStream &stream, const QListwidgetItem = nullptr; item.widgetItem = new QWidgetItemV2(toolBar); toolBar->setOrientation(floating ? ((shown & 2) ? Qt::Vertical : Qt::Horizontal) : dock.o); toolBar->setVisible(shown & 1); diff --git a/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp b/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp index 1c7ceec0eca..69965919464 100644 --- a/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp +++ b/tests/auto/widgets/widgets/qmainwindow/tst_qmainwindow.cpp @@ -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