From 10a143ccd762c810f4096a5b2e986d16ea0107ad Mon Sep 17 00:00:00 2001 From: Axel Spoerl Date: Mon, 19 Sep 2022 16:50:37 +0200 Subject: [PATCH] Prevent dock widget from resizing upon unplugging from main window QDockWidgetLayoutState::itemRect() has been used to calculate a dock widget's size when unplugging from the main window. This method is meant to calculate the size of the rubber band, showing the dock widget's dock area. The rubber band is by QDockAreaLayout::sep wider (top or bottom dock) or higher (left or right dock) than the respective dock widget. This is to make sure the rubber band is never fully covered by the dock widget. By wrongly using itemRect() also for the dock widget's size after unplugging, the dock widget grows in size each time it is unplugged. This patch passes an invalid QRect to QDockWidgetPrivate::unplug(), in order to prevent resizing. tst_QDockWidget::unplugAndResize() is extended to check size consistency after unplugging (corrected for frame margins). Fixes: QTBUG-106531 Pick-to: 6.4 6.2 Change-Id: I1cf9f695691b0e165a5cb2881781602426e5d587 Reviewed-by: Shawn Rutledge --- src/widgets/widgets/qmainwindowlayout.cpp | 4 +++- .../widgets/qdockwidget/tst_qdockwidget.cpp | 16 ++++++++++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/widgets/widgets/qmainwindowlayout.cpp b/src/widgets/widgets/qmainwindowlayout.cpp index 10732aecbe6..d5ea3d6e240 100644 --- a/src/widgets/widgets/qmainwindowlayout.cpp +++ b/src/widgets/widgets/qmainwindowlayout.cpp @@ -2654,7 +2654,9 @@ QLayoutItem *QMainWindowLayout::unplug(QWidget *widget, bool group) } else #endif // QT_CONFIG(tabwidget) { - dw->d_func()->unplug(r); + // Dock widget is unplugged from the main window + // => geometry is not supposed to change + dw->d_func()->unplug(QRect()); } } #endif // QT_CONFIG(dockwidget) diff --git a/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp b/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp index b5cf2bf42e2..68c6ee01fc4 100644 --- a/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp +++ b/tests/auto/widgets/widgets/qdockwidget/tst_qdockwidget.cpp @@ -1230,21 +1230,33 @@ void tst_QDockWidget::unplugAndResize(QMainWindow* mainWindow, QDockWidget* dw, return; } + // Remember size for comparison with unplugged object +#ifdef Q_OS_LINUX + const int pluggedWidth = dw->width(); + const int pluggedHeight = dw->height(); +#endif + // unplug and resize a dock Widget qCDebug(lcTestDockWidget) << "*** unplug and resize" << dw->objectName(); QPoint pos1 = dw->mapToGlobal(dw->rect().center()); pos1.rx() += mx; pos1.ry() += my; moveDockWidget(dw, pos1, dw->mapToGlobal(dw->rect().center())); - //QTest::mousePress(dw, Qt::LeftButton, Qt::KeyboardModifiers(), dw->mapFromGlobal(pos1)); QTRY_VERIFY(dw->isFloating()); + // Unplugged object's size may differ max. by 2x frame size +#ifdef Q_OS_LINUX + const int xMargin = 2 * dw->frameSize().width(); + const int yMargin = 2 * dw->frameSize().height(); + QVERIFY(dw->height() - pluggedHeight <= xMargin); + QVERIFY(dw->width() - pluggedWidth <= yMargin); +#endif + qCDebug(lcTestDockWidget) << "Resizing" << dw->objectName() << "to" << size; dw->setFixedSize(size); QTest::qWait(waitingTime); qCDebug(lcTestDockWidget) << "Move" << dw->objectName() << "to its home" << dw->mapFromGlobal(home); dw->move(home); - //moveDockWidget(dw, home); } bool tst_QDockWidget::checkFloatingTabs(QMainWindow* mainWindow, QPointer &ftabs, const QList &dwList) const