From 78760fd3d895b20d9286ab62c54724768f1c174b Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Tue, 16 May 2023 10:40:13 +0200 Subject: [PATCH] QDockWidget: Propagate window title when re-docking When a floating dockwidget's title changes, it is rendered as a window title. When the title changes while floating, the change will be reverted to the pre-change title when the dockwidget is docked again. This patch explicitly propagates the window title, if it has been programmatically changed while the dock widget is floating. It adds test functionality in tst_QDockWidget::floatingTabs(). Fixes: QTBUG-113591 Change-Id: I96fa69fb27ad1a85f4ea9ce44c0a22290259fca6 Reviewed-by: Volker Hilsheimer (cherry picked from commit c153066baaa88718ed45b68230d81285eb436d3d) Reviewed-by: Qt Cherry-pick Bot --- src/widgets/widgets/qdockwidget.cpp | 12 +++++++++--- .../widgets/qdockwidget/tst_qdockwidget.cpp | 17 +++++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/widgets/widgets/qdockwidget.cpp b/src/widgets/widgets/qdockwidget.cpp index c952c8d4fdc..5b9704228b5 100644 --- a/src/widgets/widgets/qdockwidget.cpp +++ b/src/widgets/widgets/qdockwidget.cpp @@ -1186,10 +1186,10 @@ void QDockWidgetPrivate::setWindowState(bool floating, bool unplug, const QRect return; // this dockwidget can't be redocked } - bool wasFloating = q->isFloating(); + const bool wasFloating = q->isFloating(); if (wasFloating) // Prevent repetitive unplugging from nested invocations (QTBUG-42818) unplug = false; - bool hidden = q->isHidden(); + const bool hidden = q->isHidden(); if (q->isVisible()) q->hide(); @@ -1497,8 +1497,14 @@ void QDockWidget::changeEvent(QEvent *event) QDockWidgetLayout *layout = qobject_cast(this->layout()); switch (event->type()) { - case QEvent::ModifiedChange: case QEvent::WindowTitleChange: + if (isFloating() && windowHandle() && d->topData()) { + // From QWidget::setWindowTitle(): Propagate window title without signal emission + d->topData()->caption = windowHandle()->title(); + d->setWindowTitle_helper(windowHandle()->title()); + } + Q_FALLTHROUGH(); + case QEvent::ModifiedChange: update(layout->titleArea()); #ifndef QT_NO_ACTION d->fixedWindowTitle = qt_setWindowTitle_helperHelper(windowTitle(), this); diff --git a/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp b/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp index 35a05f768a8..97878e08876 100644 --- a/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp +++ b/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp @@ -65,7 +65,7 @@ private slots: // Dock area permissions for DockWidgets and DockWidgetGroupWindows void dockPermissions(); - // test floating tabs and item_tree consistency + // test floating tabs, item_tree and window title consistency void floatingTabs(); // test hide & show @@ -1375,7 +1375,10 @@ void tst_QDockWidget::floatingTabs() /* * replug both dock widgets into their initial position - * expected behavior: both docks are plugged and no longer floating + * expected behavior: + - both docks are plugged + - both docks are no longer floating + - title changes have been propagated */ @@ -1396,6 +1399,12 @@ void tst_QDockWidget::floatingTabs() QTRY_VERIFY(d1->isFloating()); QTRY_VERIFY(!d2->isFloating()); + // Change titles + static constexpr QLatin1StringView newD1("New D1"); + static constexpr QLatin1StringView newD2("New D2"); + d1->setWindowTitle(newD1); + d2->setWindowTitle(newD2); + // Plug back into dock areas qCDebug(lcTestDockWidget) << "*** test plugging back to dock areas ***"; qCDebug(lcTestDockWidget) << "Move d1 to left dock"; @@ -1415,6 +1424,10 @@ void tst_QDockWidget::floatingTabs() QTRY_VERIFY(!mainWindow->findChild()); QTRY_VERIFY(ftabs.isNull()); + // check window titles + QCOMPARE(d1->windowTitle(), newD1); + QCOMPARE(d2->windowTitle(), newD2); + // Check if paths are consistent qCDebug(lcTestDockWidget) << "Checking path consistency" << layout->layoutState.indexOf(d1) << layout->layoutState.indexOf(d2);