Andrei Cioara
Andrei Cioara

Reputation: 3664

Qt Scale Application Resolution

I created a Qt Application that is guaranteed to always run on a 4:3 monitor. I hardcoded 320x240 as the resolution and designed everything to look great.

Now the requirements changed and the app will run on 640x480 resolution (still 4:3).

Is there an easy way to scale up the entire application, such that everything doubles in size. In particular I would like to avoid having to manually change all button sizes, fonts, absolute positioning for overlays, etc.

I found a lot of answers about high DPI settings, but this has little to do with high DPI.

Upvotes: 3

Views: 791

Answers (1)

Dimitry Ernot
Dimitry Ernot

Reputation: 6594

You could use a QGraphicsView and embed your main widget in it. But your whole application has to apply the same factor to all the widgets.

A QGraphicsView allows you to zoom in.

For example:

class Widget(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setFixedSize(320, 420)

        layout = QFormLayout(self)
        btn = QPushButton("Button")
        btn.setFixedSize(100, 40)
        lineEdit = QLineEdit("Button")
        btn.setFixedSize(150, 20)
        layout.addRow("A button", btn)
        layout.addRow("A Line Edit", lineEdit)



if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    w = Widget()
    view = QGraphicsView()
    scene = QGraphicsScene(0, 0, 1000, 1000)
    view.setFixedSize(320 * 2, 420 * 2)
    view.setScene(scene)
    view.scale(2, 2)
    proxy = scene.addWidget(w)
    view.centerOn(proxy)
    #w = Widget()
    #w.show()
    view.show()
    sys.exit(app.exec_())

That's the quickiest way to proceed. The best way would be to refactor your whole code to be independent of the resolution (by using a configuration class, for example).

Upvotes: 2

Related Questions