Qt Designer – Using High Resolution. Need Low Resolution version

Question:

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.

Asked By: Mike C.

||

Answers:

According to High DPI Displays documentation, the first attribute, AA_EnableHighDpiScaling should automatically scale your application for all displays. Documentation for all attributes is found here.

I’m assuming you’ve already tried something like this, where you have a boolean flag (highres) for whether the application is high resolution:

# use AA_EnableHighDpiScaling for both high and low res app versions:
if hasattr(QtCore.Qt, 'AA_EnableHighDpiScaling'):
    QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)

if highres:
    if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
        QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
else:
    if hasattr(QtCore.Qt, 'AA_UseHighDpiPixmaps'):
        QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, False)

I’m also guessing this does not give the desired result because you made your layout on a high res screen and it’s just too big to fit. You can try some trickery with Style Sheets

For your QMainWindow, you can set global values for all widgets. Assuming your main app is a QMainWindow and you drop this in init or setupUI:

if highres == False:
    style = '''QWidget {
                   fontsize: 6pt;
                   icon-size: 15px;
               }

               QWidget::indicator {
                   width: 10px;
                   height: 10px;
               }
            '''
    self.setStyleSheet(stylesheet)
Answered By: bfris

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.
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.