From 35744f666f97ac67d56be85150b66024674f3197 Mon Sep 17 00:00:00 2001 From: Tyler Goodlet Date: Thu, 18 Dec 2025 18:12:04 -0500 Subject: [PATCH] Add some Qt DPI extras to `qt_screen_info.py` - set `QT_USE_PHYSICAL_DPI='1'` env var for Qt6 high-DPI * we likely want to do this in `piker.ui` as well! - move `pxr` calc from widget to per-screen in loop. - add `unscaled_size` calc using `pxr * size`. - switch from `.availableGeometry()` to `.geometry()` for full bounds. - shorten output labels, add `!r` repr formatting - add Qt6 DPI rounding policy TODO with doc links (this patch was generated in some part by [`claude-code`][claude-code-gh]) [claude-code-gh]: https://github.com/anthropics/claude-code --- snippets/qt_screen_info.py | 43 ++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/snippets/qt_screen_info.py b/snippets/qt_screen_info.py index 9862a9b8..807e6371 100644 --- a/snippets/qt_screen_info.py +++ b/snippets/qt_screen_info.py @@ -31,6 +31,7 @@ Resource list for mucking with DPIs on multiple screens: - https://doc.qt.io/qt-5/qguiapplication.html#screenAt ''' +import os from pyqtgraph import QtGui from PyQt6 import ( @@ -44,6 +45,10 @@ from PyQt6.QtCore import ( QRect, ) + +# https://doc.qt.io/qt-6/highdpi.html#environment-variable-reference +os.environ['QT_USE_PHYSICAL_DPI'] = '1' + # Proper high DPI scaling is available in Qt >= 5.6.0. This attibute # must be set before creating the application if hasattr(Qt, 'AA_EnableHighDpiScaling'): @@ -58,13 +63,22 @@ if hasattr(Qt, 'AA_UseHighDpiPixmaps'): True, ) +# NOTE, inherits `QGuiApplication` +# https://doc.qt.io/qt-6/qapplication.html +# https://doc.qt.io/qt-6/qguiapplication.html app = QtWidgets.QApplication([]) +# +# ^TODO? various global DPI settings? +# [ ] DPI rounding policy, +# - https://doc.qt.io/qt-6/qt.html#HighDpiScaleFactorRoundingPolicy-enum +# - https://doc.qt.io/qt-6/qguiapplication.html#setHighDpiScaleFactorRoundingPolicy + window = QtWidgets.QMainWindow() main_widget = QtWidgets.QWidget() window.setCentralWidget(main_widget) window.show() -pxr: float = main_widget.devicePixelRatioF() +_main_pxr: float = main_widget.devicePixelRatioF() # explicitly get main widget and primary displays current_screen: QtGui.QScreen = app.screenAt( @@ -77,7 +91,13 @@ for screen in app.screens(): name: str = screen.name() model: str = screen.model().rstrip() size: QSize = screen.size() - geo: QRect = screen.availableGeometry() + geo: QRect = screen.geometry() + + # device-pixel-ratio + # https://doc.qt.io/qt-6/highdpi.html + pxr: float = screen.devicePixelRatio() + + unscaled_size: QSize = pxr * size phydpi: float = screen.physicalDotsPerInch() logdpi: float = screen.logicalDotsPerInch() is_primary: bool = screen is primary_screen @@ -88,11 +108,12 @@ for screen in app.screens(): f'|_primary: {is_primary}\n' f' _current: {is_current}\n' f' _model: {model}\n' - f' _screen size: {size}\n' - f' _screen geometry: {geo}\n' - f' _devicePixelRationF(): {pxr}\n' - f' _physical dpi: {phydpi}\n' - f' _logical dpi: {logdpi}\n' + f' _size: {size}\n' + f' _geometry: {geo}\n' + f' _devicePixelRatio(): {pxr}\n' + f' _unscaled-size: {unscaled_size!r}\n' + f' _physical-dpi: {phydpi}\n' + f' _logical-dpi: {logdpi}\n' ) # app-wide font info @@ -110,8 +131,8 @@ str_w: int = str_br.width() print( f'------ global font settings ------\n' - f'font dpi: {fontdpi}\n' - f'font height: {font_h}\n' - f'string bounding rect: {str_br}\n' - f'string width : {str_w}\n' + f'font dpi: {fontdpi!r}\n' + f'font height: {font_h!r}\n' + f'string bounding rect: {str_br!r}\n' + f'string width : {str_w!r}\n' )