Issue
I designed a rather large GUI using QT designer on a high resolution screen. I am needing a version to deploy on low resolution screens. In the post: PyQt GUI size on high resolution screens I found a solution for converting from low resolution to high resolution by doing this:
# Handle high resolution displays:
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
However, in my case, I need the exact opposite. I need a way to handle a GUI that was designed for a high resolution screen to scale down to low resolution.
Solution
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)
Answered By - Mike C.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.