2021-04-13 14:40:32 +08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Created on 2021/4/13
|
|
|
|
@author: Irony
|
2021-07-13 14:52:26 +08:00
|
|
|
@site: https://pyqt.site , https://github.com/PyQt5
|
2021-04-13 14:40:32 +08:00
|
|
|
@email: 892768447@qq.com
|
|
|
|
@file: ScreenNotify
|
|
|
|
@description: 屏幕、分辨率、DPI变化通知
|
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
|
2021-07-13 14:52:26 +08:00
|
|
|
try:
|
|
|
|
from PyQt5.QtCore import QTimer, QRect
|
|
|
|
from PyQt5.QtWidgets import QApplication, QPlainTextEdit
|
|
|
|
except ImportError:
|
|
|
|
from PySide2.QtCore import QTimer, QRect
|
|
|
|
from PySide2.QtWidgets import QApplication, QPlainTextEdit
|
2021-04-13 14:40:32 +08:00
|
|
|
|
|
|
|
|
|
|
|
class Window(QPlainTextEdit):
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(Window, self).__init__(*args, **kwargs)
|
|
|
|
self.appendPlainText('修改分辨率后查看')
|
|
|
|
# 记录最后一次的值(减少槽调用)
|
|
|
|
self.m_rect = QRect()
|
|
|
|
# 使用定时器来延迟触发最后一次变化
|
|
|
|
self.m_timer = QTimer(self, timeout=self.onSolutionChanged)
|
|
|
|
self.m_timer.setSingleShot(True) # **重要** 保证多次信号尽量少的调用函数
|
|
|
|
|
|
|
|
# 主要是多屏幕->无屏幕->有屏幕
|
2021-07-13 14:52:26 +08:00
|
|
|
QApplication.instance().primaryScreenChanged.connect(lambda _: self.m_timer.start(1000))
|
2021-04-13 14:40:32 +08:00
|
|
|
# 其它信号最终基本上都会调用该信号
|
2021-07-13 14:52:26 +08:00
|
|
|
QApplication.instance().primaryScreen().virtualGeometryChanged.connect(
|
|
|
|
lambda _: self.m_timer.start(1000))
|
2021-04-13 14:40:32 +08:00
|
|
|
# DPI变化
|
2021-07-13 14:52:26 +08:00
|
|
|
QApplication.instance().primaryScreen().logicalDotsPerInchChanged.connect(
|
|
|
|
lambda _: self.m_timer.start(1000))
|
2021-04-13 14:40:32 +08:00
|
|
|
|
|
|
|
def onSolutionChanged(self):
|
|
|
|
# 获取主屏幕
|
2021-07-13 14:52:26 +08:00
|
|
|
screen = QApplication.instance().primaryScreen()
|
2021-04-13 14:40:32 +08:00
|
|
|
if self.m_rect == screen.availableVirtualGeometry():
|
|
|
|
return
|
|
|
|
self.m_rect = screen.availableVirtualGeometry()
|
|
|
|
# 所有屏幕可用大小
|
|
|
|
self.appendPlainText('\navailableVirtualGeometry: {0}'.format(str(screen.availableVirtualGeometry())))
|
|
|
|
# 获取所有屏幕
|
2021-07-13 14:52:26 +08:00
|
|
|
screens = QApplication.instance().screens()
|
2021-04-13 14:40:32 +08:00
|
|
|
for screen in screens:
|
|
|
|
self.appendPlainText(
|
|
|
|
'screen: {0}, geometry({1}), availableGeometry({2}), logicalDotsPerInch({3}), '
|
|
|
|
'physicalDotsPerInch({4}), refreshRate({5})'.format(
|
|
|
|
screen.name(), screen.geometry(), screen.availableGeometry(), screen.logicalDotsPerInch(),
|
|
|
|
screen.physicalDotsPerInch(), screen.refreshRate()))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
app = QApplication(sys.argv)
|
|
|
|
w = Window()
|
|
|
|
w.show()
|
|
|
|
sys.exit(app.exec_())
|