From f54ce9617a23e70a1e10425e785d52825c73d244 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Fri, 20 Nov 2026 01:34:40 +0100 Subject: [PATCH 1/5] preview: size the Qt preview in device pixels on HiDPI displays The Qt preview set its GL viewport from the widget's logical size, but the widget's framebuffer is in device pixels. At a 200% scale the scene was drawn into the lower left quarter of the widget (#4860). The GTK preview already follows the scale factor; the Qt one did not. winfo_width/winfo_height now return device pixels, line widths and the DRO font follow the scale factor, and click-to-select scales the mouse position to match. The Qt vismach widget gets the same viewport fix. Panning converts the mouse movement, which is in logical pixels, with a new winfo_scale() hook in glnav, so the scene tracks the cursor instead of moving at 1/scale speed. It defaults to 1.0; the Qt and GTK previews override it. --- lib/python/glnav.py | 8 ++++- lib/python/qtvcp/lib/qt_vismach/qt_vismach.py | 5 +-- src/emc/usr_intf/gremlin/gremlin.py | 4 +++ src/emc/usr_intf/gremlin/qt5_graphics.py | 35 ++++++++++++++++--- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/python/glnav.py b/lib/python/glnav.py index ff8dfa6da9a..658359d8672 100644 --- a/lib/python/glnav.py +++ b/lib/python/glnav.py @@ -251,6 +251,11 @@ def reset(self): self._totalx = 0.0 self._totaly = 0.0 + def winfo_scale(self): + """Device pixels per logical (mouse event) pixel. Widgets whose + winfo_width/winfo_height are in device pixels override this.""" + return 1.0 + def recordMouse(self, x, y): self.xmouse = x self.ymouse = y @@ -301,7 +306,8 @@ def translate(self, x, y): obj = unproject((win[0], win[1] + 0.5 * win_height, win[2]), self.modelview, projection, (0, 0, win_width, win_height)) dist = math.sqrt( v3distsq( obj, obj_c ) ) - scale = abs( dist / ( 0.5 * win_height ) ) + # winfo_* are device pixels, the mouse moves in logical pixels + scale = abs( dist / ( 0.5 * win_height ) ) * self.winfo_scale() glTranslateScene(self, scale, x, y, self.xmouse, self.ymouse) # keep track of all translations since view reset diff --git a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py index 598ae1f79d1..17d7359c7ee 100644 --- a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py +++ b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py @@ -180,11 +180,12 @@ def minimumSizeHint(self): def sizeHint(self): return QSize(400, 400) + # Device pixels: the framebuffer is allocated at the window scale (HiDPI) def winfo_width(self): - return self.geometry().width() + return round(self.geometry().width() * self.devicePixelRatioF()) def winfo_height(self): - return self.geometry().height() + return round(self.geometry().height() * self.devicePixelRatioF()) def setXRotation(self, angle): angle = self.normalizeAngle(angle) diff --git a/src/emc/usr_intf/gremlin/gremlin.py b/src/emc/usr_intf/gremlin/gremlin.py index b013e4e0430..42ef7079104 100755 --- a/src/emc/usr_intf/gremlin/gremlin.py +++ b/src/emc/usr_intf/gremlin/gremlin.py @@ -264,6 +264,10 @@ def swapbuffers(self): def deactivate(self): return + def winfo_scale(self): + # Device pixels per logical (mouse event) pixel, for glnav's panning. + return self.get_scale_factor() + def winfo_width(self): # Device pixels: the area's framebuffer is allocated at the window # scale, and the viewport and pick target are sized from these. diff --git a/src/emc/usr_intf/gremlin/qt5_graphics.py b/src/emc/usr_intf/gremlin/qt5_graphics.py index c84a27f86bd..dd20e9da5db 100644 --- a/src/emc/usr_intf/gremlin/qt5_graphics.py +++ b/src/emc/usr_intf/gremlin/qt5_graphics.py @@ -27,6 +27,7 @@ import glnav from rs274 import glcanon +from rs274 import glcanon_gl from rs274 import interpret from rs274.program_time import MachineLimits, ProgramTime, format_seconds import linuxcnc @@ -406,6 +407,7 @@ def C(s): self.sf_in = "SFM: %4d" self.sf_mm = "SMM: %4d" self._font = 'monospace bold 16' + self._font_scale = None self._fontLarge = 'monospace bold 22' self._largeFontState = False self.addTimer() @@ -610,7 +612,10 @@ def set_font(self, large=None): else: font = self._font self._largeFontState = False - self.font_base, width, linespace = glnav.use_pango_font(font, 0, 128) + scale = self.winfo_scale() + self.font_base, width, linespace = glnav.use_pango_font( + font, 0, 128, scale=scale) + self._font_scale = scale self.font_linespace = linespace self.font_charwidth = width self.update() @@ -699,11 +704,28 @@ def clear_live_plotter(self): self.logger.clear() self.update() + def winfo_scale(self): + # Device pixels per logical pixel (HiDPI); may be fractional. + return self.devicePixelRatioF() + def winfo_width(self): - return self.geometry().width() + # Device pixels: the widget's framebuffer is allocated at the window + # scale, and the viewport and pick target are sized from these. + return round(self.geometry().width() * self.winfo_scale()) def winfo_height(self): - return self.geometry().height() + return round(self.geometry().height() * self.winfo_scale()) + + def update_hidpi(self): + """Follow the window's scale factor (HiDPI): the GL framebuffer is in + device pixels, so line widths and the overlay font are scaled by it. + Checked every frame, since the factor changes when the window moves + to a screen with a different scale. Needs the context. + """ + scale = self.winfo_scale() + glcanon_gl.set_line_scale(scale) + if scale != self._font_scale: + self.set_font() # trick - we are not gtk def activate(self): @@ -922,6 +944,7 @@ def paintGL(self): try: + self.update_hidpi() if self.perspective: self.redraw_perspective() else: @@ -990,7 +1013,7 @@ def redraw_ortho(self): def resizeGL(self, width, height): # redraw sets the viewport and projection from the camera each frame; # nothing to configure on the (now core) GL matrix stack here. - GL.glViewport(0, 0, width, height) + GL.glViewport(0, 0, self.winfo_width(), self.winfo_height()) #################################### # Property setting functions @@ -1069,7 +1092,9 @@ def select_fire(self): x, y = self.select_primed self.select_primed = None self.makeCurrent() - self.select(x, y) + # Events are logical pixels, the pick target is device pixels. + scale = self.winfo_scale() + self.select(x * scale, y * scale) def select_cancel(self, widget=None, event=None): self.select_primed = None From f0a64f7a868be832cdb433136fd578b4ac3c4c0b Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Fri, 20 Nov 2026 01:34:40 +0100 Subject: [PATCH 2/5] qtvcp: enable high-DPI scaling on Qt5 Qt5 leaves high-DPI scaling off unless asked. On a scaled display the fonts then follow the screen DPI while widget sizes stay in raw pixels, so point-sized text outgrows its widgets: at 192 dpi qtdragon opened at 1470x892 with most button labels clipped. Qt6 always scales. Set AA_EnableHighDpiScaling and AA_UseHighDpiPixmaps before the application is created, on Qt5 only. --- src/emc/usr_intf/qtvcp/qtvcp.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/emc/usr_intf/qtvcp/qtvcp.py b/src/emc/usr_intf/qtvcp/qtvcp.py index e4f5166fb7a..509103727a8 100644 --- a/src/emc/usr_intf/qtvcp/qtvcp.py +++ b/src/emc/usr_intf/qtvcp/qtvcp.py @@ -110,6 +110,14 @@ def __init__(self): # so web engine can load local images sys.argv.append("--disable-web-security") + # Qt5 leaves high-DPI scaling off: on a scaled display the fonts + # follow the screen DPI while widget sizes stay in raw pixels, so + # text outgrows its widgets. Qt6 always scales and has no switch. + import qtpy + if qtpy.QT5: + QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) + QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) + # initialize QApp so we can pop up dialogs now. global APP APP = MyApplication(sys.argv) From fc6e7c0de0e3b24cd3d771dee89a469be87347dd Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Sun, 6 Dec 2026 02:21:37 +0100 Subject: [PATCH 3/5] qtvcp: scale click-to-select in GCodeGraphics on HiDPI GCodeGraphics overrides select_fire and passed the mouse position to select() in logical pixels, while the pick target is in device pixels. --- lib/python/qtvcp/widgets/gcode_graphics.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/python/qtvcp/widgets/gcode_graphics.py b/lib/python/qtvcp/widgets/gcode_graphics.py index 838616812df..ab089aca291 100644 --- a/lib/python/qtvcp/widgets/gcode_graphics.py +++ b/lib/python/qtvcp/widgets/gcode_graphics.py @@ -365,7 +365,9 @@ def select_fire(self): x, y = self.select_primed self.select_primed = None self.makeCurrent() - self.select(x, y) + # Events are logical pixels, the pick target is device pixels. + scale = self.winfo_scale() + self.select(x * scale, y * scale) # override user plot -One could add gl commands to plot static objects here def user_plot(self): From 8f123eb0bf20e08ca9d9659ccbd92b1aa470f3cc Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Sun, 6 Dec 2026 02:21:37 +0100 Subject: [PATCH 4/5] qt_vismach: scale the HUD on HiDPI displays winfo_width/winfo_height are device pixels now, so the HUD kept its 1x pixel size. Render the font at the window's scale factor and lay the box out in logical pixels; rebuild the font when the factor changes. --- lib/python/qtvcp/lib/qt_vismach/primitives.py | 42 ++++++++++++++----- lib/python/qtvcp/lib/qt_vismach/qt_vismach.py | 8 +++- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/lib/python/qtvcp/lib/qt_vismach/primitives.py b/lib/python/qtvcp/lib/qt_vismach/primitives.py index 118211666f7..070d29a4003 100644 --- a/lib/python/qtvcp/lib/qt_vismach/primitives.py +++ b/lib/python/qtvcp/lib/qt_vismach/primitives.py @@ -6,7 +6,9 @@ from math import * # functions copied from glnav.py -def use_pango_font(font, start, count, will_call_prepost=False): +# scale: device pixels per logical pixel (HiDPI). The glyphs are rendered +# that much larger; the returned metrics stay in logical pixels. +def use_pango_font(font, start, count, will_call_prepost=False, scale=1.0): import gi gi.require_version('Pango','1.0') gi.require_version('PangoCairo','1.0') @@ -16,6 +18,11 @@ def use_pango_font(font, start, count, will_call_prepost=False): import cairo fontDesc = Pango.FontDescription(font) + if scale != 1.0: + if fontDesc.get_size_is_absolute(): + fontDesc.set_absolute_size(fontDesc.get_size() * scale) + else: + fontDesc.set_size(int(fontDesc.get_size() * scale)) a = array.array('b', itertools.repeat(0, 256*256)) surface = cairo.ImageSurface.create_for_data(a, cairo.FORMAT_A8, 256, 256) context = cairo.Context(surface) @@ -80,7 +87,24 @@ def use_pango_font(font, start, count, will_call_prepost=False): GL.glEndList() GL.glPopClientAttrib() - return base, int(width / Pango.SCALE), int(linespace / Pango.SCALE) + width, linespace = int(width / Pango.SCALE), int(linespace / Pango.SCALE) + if scale != 1.0: + width, linespace = ceil(width / scale), ceil(linespace / scale) + return base, width, linespace + +def hud_font(hud): + '''(Re)build a HUD's font for its window's scale factor (HiDPI) and + return that factor. The HUD is laid out in logical pixels; the window's + winfo_width/winfo_height are device pixels. Needs the GL context.''' + winfo_scale = getattr(hud.app, 'winfo_scale', None) + scale = winfo_scale() if winfo_scale else 1.0 + if not hud.fontbase or scale != getattr(hud, '_font_scale', None): + if hud.fontbase: + GL.glDeleteLists(hud.fontbase, 128) + hud.fontbase, hud._width, linespace = use_pango_font( + hud._font, 0, 128, scale=scale) + hud._font_scale = scale + return scale def pango_font_pre(rgba=(1., 1., 0., 1.)): GL.glPushAttrib(GL.GL_COLOR_BUFFER_BIT) @@ -787,11 +811,10 @@ def draw(self): GL.glPushMatrix() GL.glLoadIdentity() - if not self.fontbase: - self.fontbase, self._width, linespace = use_pango_font(self._font, 0, 128) + scale = hud_font(self) xmargin, ymargin = 5, 5 - ypos = float(self.app.winfo_height()) - winWidth = int(self.app.winfo_width()) + ypos = self.app.winfo_height() / scale + winWidth = int(self.app.winfo_width() / scale) GL.glOrtho(0.0, winWidth, 0.0, ypos, -1.0, 1.0) GL.glMatrixMode(GL.GL_MODELVIEW) @@ -937,12 +960,11 @@ def draw(self): GL.glPushMatrix() GL.glLoadIdentity() - if not self.fontbase: - self.fontbase, self._width, linespace = use_pango_font(self._font, 0, 128) + scale = hud_font(self) xmargin,ymargin = 5,5 - ypos = float(self.app.winfo_height()) - winWidth = int(self.app.winfo_width()) + ypos = self.app.winfo_height() / scale + winWidth = int(self.app.winfo_width() / scale) GL.glOrtho(0.0, winWidth, 0.0, ypos, -1.0, 1.0) GL.glMatrixMode(GL.GL_MODELVIEW) diff --git a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py index 17d7359c7ee..b0806a86d6f 100644 --- a/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py +++ b/lib/python/qtvcp/lib/qt_vismach/qt_vismach.py @@ -181,11 +181,15 @@ def sizeHint(self): return QSize(400, 400) # Device pixels: the framebuffer is allocated at the window scale (HiDPI) + def winfo_scale(self): + # Device pixels per logical pixel; may be fractional. + return self.devicePixelRatioF() + def winfo_width(self): - return round(self.geometry().width() * self.devicePixelRatioF()) + return round(self.geometry().width() * self.winfo_scale()) def winfo_height(self): - return round(self.geometry().height() * self.devicePixelRatioF()) + return round(self.geometry().height() * self.winfo_scale()) def setXRotation(self, angle): angle = self.normalizeAngle(angle) From 9df60bb992dd629a803d760a96dc37fba60034a9 Mon Sep 17 00:00:00 2001 From: Rene Hopf Date: Sun, 6 Dec 2026 02:21:37 +0100 Subject: [PATCH 5/5] qtvcp: pass fractional scale factors through on Qt5 Qt5 rounds the scale factor to an integer, so a 150% desktop became 2x. Use the PassThrough rounding policy, which is the Qt6 default. --- src/emc/usr_intf/qtvcp/qtvcp.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/emc/usr_intf/qtvcp/qtvcp.py b/src/emc/usr_intf/qtvcp/qtvcp.py index 509103727a8..d19df38a2d9 100644 --- a/src/emc/usr_intf/qtvcp/qtvcp.py +++ b/src/emc/usr_intf/qtvcp/qtvcp.py @@ -117,6 +117,13 @@ def __init__(self): if qtpy.QT5: QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps) + # Qt5 rounds the factor to an integer (150% becomes 2x); pass + # it through unrounded as Qt6 does. Needs Qt 5.14. + try: + QtGui.QGuiApplication.setHighDpiScaleFactorRoundingPolicy( + QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) + except AttributeError: + pass # initialize QApp so we can pop up dialogs now. global APP