切换到PySide6,感觉之前的AA_EnableHighDpiScaling不起作用了
经过一番查询,在官方的github找到了一点说明,Class/Function Deprecations
The High DPI scaling attributes Qt.AA_EnableHighDpiScaling, Qt.AA_DisableHighDpiScaling and Qt.AA_UseHighDpiPixmaps are deprecated. High DPI is by default enabled in Qt 6 and cannot be turned off.
也就是说从6开始,这三个高分屏缩放的API都无了,默认强制开启以及不能关闭,不过问题是并没有感觉到它缩放ORZ
最后在文档中查询到有API是可以调整缩放策略的,搜索HighDpiScaleFactorRoundingPolicy就能找到
- Qt.HighDpiScaleFactorRoundingPolicy.Round 设备像素比0.5及以上的,进行缩放
- Qt.HighDpiScaleFactorRoundingPolicy.Ceil 始终缩放
- Qt.HighDpiScaleFactorRoundingPolicy.Floor 始终不缩放
- Qt.HighDpiScaleFactorRoundingPolicy.RoundPreferFloor 设备像素比0.75及以上的,进行缩放
- Qt.HighDpiScaleFactorRoundingPolicy.PassThrough 不缩放
也许你看完可能还是有些不明白,一开始我也是
然后找到官方widgetgallery.py这个案例,取了部分代码,如下,可以配合这个运行你的GUI,看看各个情况的效果
def format_geometry(rect):
"""Format a geometry as a X11 geometry specification"""
return "{}x{}{:+d}{:+d}".format(rect.width(), rect.height(), rect.x(), rect.y())
def screen_info(widget):
"""Format information on the screens"""
policy = QGuiApplication.highDpiScaleFactorRoundingPolicy()
policy_string = str(policy).split('.')[-1]
result = "<p>High DPI scale factor rounding policy: {}</p><ol>".format(policy_string)
for screen in QGuiApplication.screens():
current = screen == widget.screen()
result += "<li>"
if current:
result += "<i>"
result += '"{}" {} {}DPI, DPR={}'.format(screen.name(), format_geometry(screen.geometry()), int(screen.logicalDotsPerInchX()), screen.devicePixelRatio())
if current:
result += "</i>"
result += "</li>"
result += "</ol>"
return result
用这里得到的结果,显示策略是PassThrough
,难怪没感觉到缩放(4K+win10+150%)
<p>High DPI scale factor rounding policy: PassThrough</p><ol><li><i>"\\.\DISPLAY1" 2560x1440+0+0 96DPI, DPR=1.5</i></li></ol>
最终我选择了始终缩放的策略,通过下面的代码即可设定,注意代码应该放到gui界面实例化之前
QGuiApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.Ceil)