Pyqt Gui Size on High Resolution Screens

Qt Designer - Using High Resolution. Need Low Resolution version

I ended up writing a small program to change the ratio within the ui file that was generated by Qt Designer. There are only four tags that I changed , , , and . This got it pretty close so that now I am able to tweak it with designer. I hope this helps someone else.

old_file = "old.ui"
new_file = "new.ui"
#Old resolution
oldx = 3840.0
oldy = 2160.0
#New resolution
newx = 1360.0
newy = 768.0

ratiox = newx / oldx
ratioy = newy / oldy

def find_between( s, first, last ):
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]

with open(old_file, 'r') as f:
for line in f:
if "<width>" in line:
num = float(find_between(line, "<width>", "</width>"))
#Fix resolution
old_size = str(int(num))
new_size = str(int(num * ratiox))
w = line.replace(old_size, new_size)
elif "<height>" in line:
num = float(find_between(line, "<height>", "</height>"))
#Fix resolution
old_size = str(int(num))
new_size = str(int(num * ratioy))
w = line.replace(old_size, new_size)
elif "<x>" in line:
num = float(find_between(line, "<x>", "</x>"))
#Fix resolution
old_size = str(int(num))
new_size = str(int(num * ratiox))
w = line.replace(old_size, new_size)
elif "<y>" in line:
num = float(find_between(line, "<y>", "</y>"))
#Fix resolution
old_size = str(int(num))
new_size = str(int(num * ratioy))
w = line.replace(old_size, new_size)
else:
w = line
with open('new_file.ui', 'a') as g:
g.write(w)

How can I resize the main window depending on screen resolution using PyQt

You can use

showFullScreen() or just showMaximized()

and you can get screen geometry with:

desktop() and screenGeometry()

PyQt5 Resize app for different displays

This is somewhat system dependent, so it would help if you mentioned your target platform(s).

Because PyQt5 is just a wrapper around Qt5, I think High DPI Displays from the Qt manual applies. Citing the relevant bit (but you should read the whole thing):

In order to get an application designed for low DPI values running on a high resolution monitors quickly, consider one of the scaling options (let the application run as DPI Unaware on Windows or set the environment variable QT_AUTO_SCREEN_SCALE_FACTOR to "1". These options may incur some scaling or painting artifacts, though.

In the longer term, the application should be adapted to run unmodified:

  • Always use the qreal versions of the QPainter drawing API.
  • Size windows and dialogs in relation to the screen size.
  • Replace hard-coded sizes in layouts and drawing code by values calculated from font metrics or screen size.

In a shell, you would do something like:

$ export QT_AUTO_SCREEN_SCALE_FACTOR=1
$ python my_application.py


Related Topics



Leave a reply



Submit