我正在使用许多小部件(QGroupBox,QVBoxLayout,QHBoxLayout)的应用程序上工作。最初,它是在普通高清监视器上开发的。但是,最近我们许多人将其显示器升级为4K分辨率。现在,一些按钮,滑块被压缩得很小,无法使用。
现在我尝试进行一些更改,以便可以在HD和4K显示器中使用该应用程序。
我开始阅读下面的链接
https://leomoon.com/journal/python/high-dpi-scaling-in-pyqt5/ enter link description here
我以为只要在特定的监视器中打开窗口,我就可以调用特定的行代码
if pixel_x > 1920 and pixel_y > 1080:
Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, True)
Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
else:
Qapp.setAttribute(Qt.AA_EnableHighDpiScaling, False)
Qapp.setAttribute(Qt.AA_UseHighDpiPixmaps, False)
现在,我尝试通过使用相关文章here使用以下代码来计算pixel_x和pixel_y。
import sys, ctypes
user32 = ctypes.windll.user32
user32.SetProcessDPIAware()
screen_width = 0 #78
screen_height = 1 #79
[pixel_x , pixel_y ] = [user32.GetSystemMetrics(screen_width), user32.GetSystemMetrics(screen_height)]
screen_width = 0,screen_height = 1为我提供了主显示器(在我们的情况下,大多数是高清笔记本电脑)的分辨率。 screen_width = 78,screen_height = 79为我提供了虚拟机的综合分辨率。但是,我不明白如何根据打开应用程序的位置动态获取这些值。
我的应用程序窗口的开发方式是,它将在上次关闭的同一监视器中打开。现在的问题是,我想在调用GUI时获得活动的监视器分辨率,并适应该分辨率。如果有人可以帮助我,我会感到很高兴。
我有兴趣知道每次将窗口从HD拖动到4K时都可以调用屏幕分辨率计算,反之亦然。
编辑:我在这篇文章here中发现了类似的东西,但是我从中不能得到很多。
Edit2:基于@Joe解决方案,基于主屏幕检测,尽管我以4K屏幕运行应用程序,为什么主屏幕始终是笔记本电脑的分辨率
我刚刚尝试使用以下代码获取所有屏幕的dpi
def screen_selection():
app = QApplication(sys.argv)
valid_screens = []
for index, screen_no in enumerate(app.screens()):
screen = app.screens()[index]
dpi = screen.physicalDotsPerInch()
valid_screens.append(dpi)
return valid_screens
我来到across的一个解决方案是使用临时QApplication()
:
import sys
from PyQt5 import QtWidgets, QtCore, QtGui
# fire up a temporary QApplication
def get_resolution():
app = QtWidgets.QApplication(sys.argv)
print(app.primaryScreen())
d = app.desktop()
print(d.screenGeometry())
print(d.availableGeometry())
print(d.screenCount())
g = d.screenGeometry()
return (g.width(), g.height())
x, y = get_resolution()
if x > 1920 and y > 1080:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
else:
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, False)
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, False)
# Now your code ...
此功能将检测所有连接的屏幕:
# fire up a temporary QApplication
def get_resolution_multiple_screens():
app = QtGui.QGuiApplication(sys.argv)
#QtWidgets.QtGui
all_screens = app.screens()
for s in all_screens:
print()
print(s.name())
print(s.availableGeometry())
print(s.availableGeometry().width())
print(s.availableGeometry().height())
print(s.size())
print(s.size().width())
print(s.size().height())
print()
print('primary:', app.primaryScreen())
print('primary:', app.primaryScreen().availableGeometry().width())
print('primary:', app.primaryScreen().availableGeometry().height())
# now choose one
您可以使用提示here和here来获取正在运行应用程序的屏幕。
但是我认为primaryScreen
也应该返回此:
primaryScreen:QScreen * const
此属性保存主屏幕(或默认屏幕)申请。
这将是最初显示QWindows的屏幕,除非另有规定。
(https://doc.qt.io/qt-5/qguiapplication.html#primaryScreen-prop)
嗯,在创建MainWindow之后,您只需调用QMainWindow.screen()。这将返回MainWindow处于打开状态的当前屏幕。这至少可以让您在应用程序启动时检查屏幕分辨率。现在没有screenChangeEvent之类的东西。但是我确信您可以通过子类化MainWindow并重载QMainWindow.changeEvent()来创建一个。