diff --git a/src/gui/kernel/qguiapplication.cpp b/src/gui/kernel/qguiapplication.cpp index d26de0b07cb..422c6ee8f69 100644 --- a/src/gui/kernel/qguiapplication.cpp +++ b/src/gui/kernel/qguiapplication.cpp @@ -2565,8 +2565,6 @@ void QGuiApplicationPrivate::processWindowScreenChangedEvent(QWindowSystemInterf if (window->screen() == wse->screen.data()) return; - const qreal oldDevicePixelRatio = window->screen() ? window->screen()->devicePixelRatio() : 1.0; - if (QWindow *topLevelWindow = window->d_func()->topLevelWindow(QWindow::ExcludeTransients)) { if (QScreen *screen = wse->screen.data()) topLevelWindow->d_func()->setTopLevelScreen(screen, false /* recreate */); @@ -2583,12 +2581,7 @@ void QGuiApplicationPrivate::processWindowScreenChangedEvent(QWindowSystemInterf processGeometryChangeEvent(&gce); } #endif - - const qreal newDevicePixelRatio = window->screen() ? window->screen()->devicePixelRatio() : 1.0; - if (!qFuzzyCompare(oldDevicePixelRatio, newDevicePixelRatio)) { - QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); - QGuiApplication::sendSpontaneousEvent(window, &dprChangeEvent); - } + QWindowPrivate::get(window)->updateDevicePixelRatio(); } } @@ -2596,9 +2589,7 @@ void QGuiApplicationPrivate::processWindowDevicePixelRatioChangedEvent(QWindowSy { if (wde->window.isNull()) return; - - QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); - QGuiApplication::sendSpontaneousEvent(wde->window, &dprChangeEvent); + QWindowPrivate::get(wde->window)->updateDevicePixelRatio(); } void QGuiApplicationPrivate::processSafeAreaMarginsChangedEvent(QWindowSystemInterfacePrivate::SafeAreaMarginsChangedEvent *wse) diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp index 2f775ec6648..81a4469a9ef 100644 --- a/src/gui/kernel/qwindow.cpp +++ b/src/gui/kernel/qwindow.cpp @@ -213,8 +213,10 @@ void QWindowPrivate::init(QScreen *targetScreen) isWindow = true; parentWindow = static_cast(q->QObject::parent()); + QScreen *connectScreen = targetScreen ? targetScreen : QGuiApplication::primaryScreen(); + if (!parentWindow) - connectToScreen(targetScreen ? targetScreen : QGuiApplication::primaryScreen()); + connectToScreen(connectScreen); // If your application aborts here, you are probably creating a QWindow // before the screen list is populated. @@ -224,6 +226,7 @@ void QWindowPrivate::init(QScreen *targetScreen) QGuiApplicationPrivate::window_list.prepend(q); requestedFormat = QSurfaceFormat::defaultFormat(); + devicePixelRatio = connectScreen->devicePixelRatio(); } /*! @@ -505,8 +508,6 @@ void QWindowPrivate::create(bool recursive, WId nativeHandle) // the platformWindow, if there was one, is now gone, so make this flag reflect reality now updateRequestPending = false; - const qreal currentDevicePixelRatio = q->devicePixelRatio(); - if (q->parent()) q->parent()->create(); @@ -552,10 +553,7 @@ void QWindowPrivate::create(bool recursive, WId nativeHandle) QPlatformSurfaceEvent e(QPlatformSurfaceEvent::SurfaceCreated); QGuiApplication::sendEvent(q, &e); - if (!qFuzzyCompare(currentDevicePixelRatio, q->devicePixelRatio())) { - QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); - QGuiApplication::sendEvent(q, &dprChangeEvent); - } + updateDevicePixelRatio(); if (needsUpdate) q->requestUpdate(); @@ -1333,14 +1331,29 @@ Qt::ScreenOrientation QWindow::contentOrientation() const qreal QWindow::devicePixelRatio() const { Q_D(const QWindow); + return d->devicePixelRatio; +} + +/* + Updates the cached devicePixelRatio value by polling for a new value. + Sends QEvent::DevicePixelRatioChange to the window if the DPR has changed. +*/ +void QWindowPrivate::updateDevicePixelRatio() +{ + Q_Q(QWindow); // If there is no platform window use the associated screen's devicePixelRatio, // which typically is the primary screen and will be correct for single-display // systems (a very common case). - if (!d->platformWindow) - return screen()->devicePixelRatio(); + const qreal newDevicePixelRatio = platformWindow ? + platformWindow->devicePixelRatio() * QHighDpiScaling::factor(q) : q->screen()->devicePixelRatio(); - return d->platformWindow->devicePixelRatio() * QHighDpiScaling::factor(this); + if (newDevicePixelRatio == devicePixelRatio) + return; + + devicePixelRatio = newDevicePixelRatio; + QEvent dprChangeEvent(QEvent::DevicePixelRatioChange); + QGuiApplication::sendEvent(q, &dprChangeEvent); } Qt::WindowState QWindowPrivate::effectiveState(Qt::WindowStates state) diff --git a/src/gui/kernel/qwindow_p.h b/src/gui/kernel/qwindow_p.h index ef633d093a1..96beb17becf 100644 --- a/src/gui/kernel/qwindow_p.h +++ b/src/gui/kernel/qwindow_p.h @@ -89,6 +89,8 @@ public: void setAutomaticPositionAndResizeEnabled(bool a) { positionAutomatic = resizeAutomatic = a; } + void updateDevicePixelRatio(); + static QWindowPrivate *get(QWindow *window) { return window->d_func(); } static Qt::WindowState effectiveState(Qt::WindowStates); @@ -106,6 +108,7 @@ public: QString windowFilePath; QIcon windowIcon; QRect geometry; + qreal devicePixelRatio = 1.0; Qt::WindowStates windowState = Qt::WindowNoState; QWindow::Visibility visibility = QWindow::Hidden; bool resizeEventPending = true;