Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update qt screen info script #450

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 52 additions & 51 deletions snippets/qt_screen_info.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
"""
Resource list for mucking with DPIs on multiple screens:

- https://stackoverflow.com/questions/42141354/convert-pixel-size-to-point-size-for-fonts-on-multiple-platforms
- https://stackoverflow.com/questions/25761556/qt5-font-rendering-different-on-various-platforms/25929628#25929628
- https://doc.qt.io/qt-5/highdpi.html
- https://stackoverflow.com/questions/20464814/changing-dpi-scaling-size-of-display-make-qt-applications-font-size-get-rendere
- https://stackoverflow.com/a/20465247
- https://doc.qt.io/archives/qt-4.8/qfontmetrics.html#width
- https://forum.qt.io/topic/54136/how-do-i-get-the-qscreen-my-widget-is-on-qapplication-desktop-screen-returns-a-qwidget-and-qobject_cast-qscreen-returns-null/3
- https://forum.qt.io/topic/43625/point-sizes-are-they-reliable/4
- https://stackoverflow.com/questions/16561879/what-is-the-difference-between-logicaldpix-and-physicaldpix-in-qt
- https://doc.qt.io/qt-5/qguiapplication.html#screenAt

DPI and info helper script for display metrics.
"""

from pyqtgraph import QtGui
# Resource list for mucking with DPIs on multiple screens:
# https://stackoverflow.com/questions/42141354/convert-pixel-size-to-point-size-for-fonts-on-multiple-platforms
# https://stackoverflow.com/questions/25761556/qt5-font-rendering-different-on-various-platforms/25929628#25929628
# https://doc.qt.io/qt-5/highdpi.html
# https://stackoverflow.com/questions/20464814/changing-dpi-scaling-size-of-display-make-qt-applications-font-size-get-rendere
# https://stackoverflow.com/a/20465247
# https://doc.qt.io/archives/qt-4.8/qfontmetrics.html#width
# https://forum.qt.io/topic/54136/how-do-i-get-the-qscreen-my-widget-is-on-qapplication-desktop-screen-returns-a-qwidget-and-qobject_cast-qscreen-returns-null/3
# https://forum.qt.io/topic/43625/point-sizes-are-they-reliable/4
# https://stackoverflow.com/questions/16561879/what-is-the-difference-between-logicaldpix-and-physicaldpix-in-qt
# https://doc.qt.io/qt-5/qguiapplication.html#screenAt

from pyqtgraph import (
QtGui,
)
from PyQt5.QtCore import (
Qt, QCoreApplication
Qt,
QCoreApplication,
)
from PyQt5.QtWidgets import (
QWidget,
QMainWindow,
QApplication,
)

# Proper high DPI scaling is available in Qt >= 5.6.0. This attibute
Expand All @@ -28,55 +36,48 @@
QCoreApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)


app = QtGui.QApplication([])
window = QtGui.QMainWindow()
main_widget = QtGui.QWidget()
app = QApplication([])
window = QMainWindow()
main_widget = QWidget()
window.setCentralWidget(main_widget)
window.show()

# TODO: move widget through multiple displays and auto-detect the pixel
# ratio? (probably is gonna require calls to i3ipc on linux)..
pxr = main_widget.devicePixelRatioF()

# screen_num = app.desktop().screenNumber()
# TODO: how to detect list of displays from API?
# screen = app.screens()[screen_num]

screen = app.screenAt(main_widget.geometry().center())

name = screen.name()
size = screen.size()
geo = screen.availableGeometry()
phydpi = screen.physicalDotsPerInch()
logdpi = screen.logicalDotsPerInch()

print(
# f'screen number: {screen_num}\n',
f'screen name: {name}\n'
f'screen size: {size}\n'
f'screen geometry: {geo}\n\n'
f'devicePixelRationF(): {pxr}\n'
f'physical dpi: {phydpi}\n'
f'logical dpi: {logdpi}\n'
)
def ppscreeninfo(screen: QtGui.QScreen) -> None:
# screen_num = app.desktop().screenNumber()
name = screen.name()
size = screen.size()
geo = screen.availableGeometry()
phydpi = screen.physicalDotsPerInch()
logdpi = screen.logicalDotsPerInch()
rr = screen.refreshRate()

print('-'*50)
print(
# f'screen number: {screen_num}\n',
f'screen: {name}\n'
f' size: {size}\n'
f' geometry: {geo}\n'
f' logical dpi: {logdpi}\n'
f' devicePixelRationF(): {pxr}\n'
f' physical dpi: {phydpi}\n'
f' refresh rate: {rr}\n'
)

screen = app.primaryScreen()
print('-'*50 + '\n')

name = screen.name()
size = screen.size()
geo = screen.availableGeometry()
phydpi = screen.physicalDotsPerInch()
logdpi = screen.logicalDotsPerInch()

print(
# f'screen number: {screen_num}\n',
f'screen name: {name}\n'
f'screen size: {size}\n'
f'screen geometry: {geo}\n\n'
f'devicePixelRationF(): {pxr}\n'
f'physical dpi: {phydpi}\n'
f'logical dpi: {logdpi}\n'
)
screen = app.screenAt(main_widget.geometry().center())
ppscreeninfo(screen)

screen = app.primaryScreen()
ppscreeninfo(screen)

# app-wide font
font = QtGui.QFont("Hack")
Expand Down