Skip to content

Qt preview and qtvcp: follow the display scale on HiDPI - #4871

Open
rene-dev wants to merge 2 commits into
LinuxCNC:masterfrom
rene-dev:qt-preview-hidpi
Open

rene-dev wants to merge 2 commits into
LinuxCNC:masterfrom
rene-dev:qt-preview-hidpi

Conversation

@rene-dev

Copy link
Copy Markdown
Member

Fixes #4860.

Preview drawn into a quarter of the widget

The Qt preview set its GL viewport from the widget's logical size, but the framebuffer is in device pixels. At 200% the scene landed in the lower left quarter, as in the issue's qtdragon screenshots. The GTK preview already follows the scale factor; this brings the Qt one in line:

  • winfo_width/winfo_height return device pixels
  • line widths and the DRO font follow the scale factor, checked each frame
  • click-to-select scales the mouse position
  • the Qt vismach widget gets the same viewport fix
  • panning converts the mouse movement through a new winfo_scale() hook in glnav (default 1.0), overridden by the Qt and GTK previews, so the scene tracks the cursor instead of moving at 1/scale speed

Oversized text on Qt5

Qt5 leaves high-DPI scaling off, so on a scaled display fonts follow the screen DPI while widget sizes stay in raw pixels. At 192 dpi qtdragon opened at 1470x892 with most button labels clipped. qtvcp now sets AA_EnableHighDpiScaling and AA_UseHighDpiPixmaps on Qt5 only; Qt6 always scales.

Testing

PyQt5 5.15 under XWayland at 192 dpi:

  • qtdragon, qtdragon_lathe, qtaxis: preview fills its widget, labels fit
  • standalone sim_panel, test_panel, vismach_mill_xyz: fine, vismach fills its window

Not tested: Qt6, panning and click-to-select by hand, and the GTK panning change (it returns 1 at scale 1, so nothing changes there).

Known issue

A qtvcp panel embedded in AXIS with -x (configs/sim/axis/qtvcp/qtvismach_tab.ini) is now 1600x1200 instead of 800x600 at 192 dpi and overflows its tab, since AXIS does not resize the embedded panel. Not addressed here.

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 (LinuxCNC#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.
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.
self.select(x, y)
# Events are logical pixels, the pick target is device pixels.
scale = self.winfo_scale()
self.select(x * scale, y * scale)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

qtvcp's GCodeGraphics overrides select_fire in lib/python/qtvcp/widgets/gcode_graphics.py and still calls self.select(x, y), so qtdragon and qtaxis don't get this scaling. At 200% clicking a program line selects nothing in qtdragon; with the scaling in the override it works. Could the override get it too, or call this one after its own checks?

import qtpy
if qtpy.QT5:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Qt5 rounds the scale factor to the nearest integer, so a 150% desktop becomes 2x and a 1920x1080 screen looks like 960x540 to qtvcp. Adding QtGui.QGuiApplication.setHighDpiScaleFactorRoundingPolicy(QtCore.Qt.HighDpiScaleFactorRoundingPolicy.PassThrough) here gives 1.5, same as Qt6's default. Worth adding?

# 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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With winfo_* now in device pixels, the vismach HUD text (primitives.py) stays at its 1x pixel size, so at 200% it is half size relative to the window. Maybe pass scale= to use_pango_font there as in qt5_graphics?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Qtvcp interfaces not scaling correctly with Wayland (ex. 200% scale)

2 participants