aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickwidgets
diff options
context:
space:
mode:
authorMitch Curtis <mitch.curtis@qt.io>2020-03-05 16:00:04 +0100
committerMitch Curtis <mitch.curtis@qt.io>2020-03-24 13:57:22 +0100
commit7be805d155f48b2ee9dfcb80ae2919597d0adf5a (patch)
treea2353452c3c9a0b4cdbbf8c15194d531cae042a6 /src/quickwidgets
parent7bef20343837c6c44fa194fd2f361c12cd1c2466 (diff)
Resize offscreen window when QQuickWidget is resized
In a typical Qt Quick application, when a window is resized, the contentItem of that window is resized with it, and then the root item. QQuickOverlay in qtquickcontrols2 listens to size changes in the contentItem (QQuickRootItem) via addItemChangeListener(), as a cheap way (e.g. no signals) of knowing when to resize background dimming effects. It resizes the dimmer item to the size of the window. The first problem with QQuickWidget is that it only ever resizes the root item when using the SizeRootObjectToView resize mode, and not the contentItem. The second problem is that the root item is resized (via updateSize()) before the window itself even has a size (which happens in QQuickWidget::createFramebufferObject() via the call to d->offscreenWindow->setGeometry()). To demonstrate the second problem in detail, consider the following widget hierarchy (written in everybody's favorite language: QML): QMainWindow { QQuickWidget { QQuickWindow { // QQuickWidgetPrivate::offscreenWindow QQuickRootItem { // QQuickWindowPrivate::contentItem Page {} // QQuickWidgetPrivate::root } } } } The QMainWindow starts off as 200x200. When the window is resized, QQuickWidget::resizeEvent() is called. The first thing it does is call updateSize(), which in the case of SizeRootObjectToView, resizes the root item to 300x300. This causes QQuickOverlayPrivate::itemGeometryChanged() to be called, and the dimmers are resized to the size of the window, but the window still has its 200x200 size, as it is only updated later, when QQuickWidget::createFramebufferObject() is called. This patch fixes these issues by ensuring that contentItem and the window itself are resized along with the root item. As to why such manual intervention is necessary: from what I can see, it is because it's an "offscreen" window. This means that QWindowPrivate::platformWindow is null, and setGeometry() takes a different path that presumably results in no QResizeEvent being sent to the QQuickWindow. As QQuickWindow relies on resizeEvent() being called to resize its contentItem, the contentItem is never resized. With a typical Qt Quick application, all of this works as expected. Change-Id: I7401aa7a9b209096183416ab53014f67cceccbe4 Fixes: QTBUG-78323 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> Reviewed-by: Laszlo Agocs <laszlo.agocs@qt.io> Reviewed-by: Paul Olav Tvete <paul.tvete@qt.io>
Diffstat (limited to 'src/quickwidgets')
-rw-r--r--src/quickwidgets/qquickwidget.cpp33
1 files changed, 24 insertions, 9 deletions
diff --git a/src/quickwidgets/qquickwidget.cpp b/src/quickwidgets/qquickwidget.cpp
index 8ecfbf65dd..35cf06927a 100644
--- a/src/quickwidgets/qquickwidget.cpp
+++ b/src/quickwidgets/qquickwidget.cpp
@@ -107,6 +107,7 @@ void QQuickWidgetPrivate::init(QQmlEngine* e)
renderControl = new QQuickWidgetRenderControl(q);
offscreenWindow = new QQuickWindow(*new QQuickOffcreenWindowPrivate(),renderControl);
offscreenWindow->setTitle(QString::fromLatin1("Offscreen"));
+ offscreenWindow->setObjectName(QString::fromLatin1("QQuickOffScreenWindow"));
// Do not call create() on offscreenWindow.
// Check if the Software Adaptation is being used
@@ -802,15 +803,29 @@ void QQuickWidgetPrivate::updateSize()
q->updateGeometry();
}
} else if (resizeMode == QQuickWidget::SizeRootObjectToView) {
- bool needToUpdateWidth = !qFuzzyCompare(q->width(), root->width());
- bool needToUpdateHeight = !qFuzzyCompare(q->height(), root->height());
-
- if (needToUpdateWidth && needToUpdateHeight)
- root->setSize(QSizeF(q->width(), q->height()));
- else if (needToUpdateWidth)
- root->setWidth(q->width());
- else if (needToUpdateHeight)
- root->setHeight(q->height());
+ const bool needToUpdateWidth = !qFuzzyCompare(q->width(), root->width());
+ const bool needToUpdateHeight = !qFuzzyCompare(q->height(), root->height());
+
+ if (needToUpdateWidth && needToUpdateHeight) {
+ // Make sure that we have realistic sizing behavior by following
+ // what on-screen windows would do and resize everything, not just
+ // the root item. We do this because other types may be relying on
+ // us to behave correctly.
+ const QSizeF newSize(q->width(), q->height());
+ offscreenWindow->resize(newSize.toSize());
+ offscreenWindow->contentItem()->setSize(newSize);
+ root->setSize(newSize);
+ } else if (needToUpdateWidth) {
+ const int newWidth = q->width();
+ offscreenWindow->setWidth(newWidth);
+ offscreenWindow->contentItem()->setWidth(newWidth);
+ root->setWidth(newWidth);
+ } else if (needToUpdateHeight) {
+ const int newHeight = q->height();
+ offscreenWindow->setHeight(newHeight);
+ offscreenWindow->contentItem()->setHeight(newHeight);
+ root->setHeight(newHeight);
+ }
}
}