Qt Modifications

This post is for the software devs out there. Modern CSV is written in C++ and uses the Qt framework. I finally had to modify Qt to get something to work. This blog post will be where I put all Qt modifications for compliance with the LPGL license and for the benefit of my fellow Qt developers.

Windows Mixed DPI Issue

In Windows, I got rid of the native chrome to make it look more modern in accordance with its namesake. However, there was an issue when using two or more monitors with different DPIs. Qt and Windows were fighting with each other to determine the DPI and it was causing problems. Ultimately, I had to modify Qt's source code. It was a succinct fix and only affects Qt6Gui.dll. This is on Qt 6.6.3. Its DLL is included starting in version 2.4.5. The commit and entire qtbase code can be found at https://github.com/evanemm/qtbase/tree/windows-fix-mixed-dpi. Here's the diff:

diff --git a/src/gui/kernel/qhighdpiscaling_p.h b/src/gui/kernel/qhighdpiscaling_p.h
index 189f31fd0a8..b461589538b 100644
--- a/src/gui/kernel/qhighdpiscaling_p.h
+++ b/src/gui/kernel/qhighdpiscaling_p.h
@@ -25,6 +25,7 @@
 #include <QtGui/qscreen.h>
 #include <QtGui/qvector2d.h>
 #include <QtGui/qwindow.h>
+#include <qpa/qplatformwindow.h>

 QT_BEGIN_NAMESPACE

@@ -255,7 +256,9 @@ template <typename T, typename C>
 T fromNativeWindowGeometry(const T &value, const C *context)
 {
     QHighDpiScaling::ScaleAndOrigin so = QHighDpiScaling::scaleAndOrigin(context);
-    QPoint effectiveOrigin = (context && context->isTopLevel()) ? so.origin : QPoint(0,0);
+    const bool topLevel = context && context->isTopLevel()
+            && !(context->handle() && context->handle()->isEmbedded());
+    QPoint effectiveOrigin = topLevel ? so.origin : QPoint(0,0);
     return scale(value, qreal(1) / so.factor, effectiveOrigin);
 }

@@ -263,7 +266,9 @@ template <typename T, typename C>
 T toNativeWindowGeometry(const T &value, const C *context)
 {
     QHighDpiScaling::ScaleAndOrigin so = QHighDpiScaling::scaleAndOrigin(context);
-    QPoint effectiveOrigin = (context && context->isTopLevel()) ? so.origin : QPoint(0,0);
+    const bool topLevel = context && context->isTopLevel()
+            && !(context->handle() && context->handle()->isEmbedded());
+    QPoint effectiveOrigin = topLevel ? so.origin : QPoint(0,0);
     return scale(value, so.factor, effectiveOrigin);
 }

diff --git a/src/gui/kernel/qplatformwindow.cpp b/src/gui/kernel/qplatformwindow.cpp
index e2c9fc4141c..3b59a339c36 100644
--- a/src/gui/kernel/qplatformwindow.cpp
+++ b/src/gui/kernel/qplatformwindow.cpp
@@ -551,7 +551,7 @@ QPlatformScreen *QPlatformWindow::screenForGeometry(const QRect &newGeometry) co
     // QRect::center can return a value outside the rectangle if it's empty.
     // Apply mapToGlobal() in case it is a foreign/embedded window.
     QPoint center = newGeometry.isEmpty() ? newGeometry.topLeft() : newGeometry.center();
-    if (isForeignWindow())
+    if (isForeignWindow() || isEmbedded())
         center = mapToGlobal(center - newGeometry.topLeft());

     if (!parent() && currentScreen && !currentScreen->geometry().contains(center)) {
diff --git a/src/gui/kernel/qwindow.cpp b/src/gui/kernel/qwindow.cpp
index 1bc35e9524d..812806c85d7 100644
--- a/src/gui/kernel/qwindow.cpp
+++ b/src/gui/kernel/qwindow.cpp
@@ -1822,6 +1822,10 @@ QScreen *QWindowPrivate::screenForGeometry(const QRect &newGeometry) const
 {
     Q_Q(const QWindow);
     QScreen *currentScreen = q->screen();
+    // Embedded window geometry is relative to its native parent, not the desktop.
+    // The platform plugin updates the screen when the native parent changes screens.
+    if (platformWindow && platformWindow->isEmbedded())
+        return currentScreen;
     QScreen *fallback = currentScreen;
     QPoint center = newGeometry.center();
     if (!q->parent() && currentScreen && !currentScreen->geometry().contains(center)) {