Qt: Qwidget Size

I am writing a program with Qt that looks like this:

screen capture

The main window is the Window : QWidget class that I defined. It has a QGridLayout , which has basically 1 row and 3 columns. As you can see, the first column contains the menu (this is the Menu :QWidget class), the second and third columns contain the canvas (the Canvas : QWidget class, which I also defined).

It's very hard for me to try to figure out how dimensions work. So far, I just determined the minimum size for the first column width (in my window layout) to fix the menu width, and I set a fixed size for Canvas in its constructor (for example: setFixedSize(QSize(size_in_pixels, size_in_pixels)); ).

The problem, of course, is that this does not work well for the user to scale the window. I suppose what I would like to do is somehow set my canvas sizeHint to size_in_pixels (my preferred size), but this is hardly possible. I would also like my canvases to have the same height and width. I read the Qt documentation and tried a few things, but I cannot come up with a solution.

What will be the way for you? Thanks so much for your ideas.

+4
source share
1 answer

You can simply install canvas widgets in an Expanding size policy. They will equally consume the remaining space as the size of the layout increases.

Here is an example written in PyQt4

 widget = QtGui.QWidget() widget.resize(800,600) layout = QtGui.QGridLayout(widget) label = QtGui.QLabel("Menu") label.setFrameStyle(label.Box) label.setMinimumWidth(100) layout.addWidget(label, 0, 0) label = QtGui.QLabel("Canvas 1") label.setFrameStyle(label.Box) policy = QtGui.QSizePolicy( QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) label.setSizePolicy(policy) layout.addWidget(label, 0, 1) label = QtGui.QLabel("Canvas 2") label.setFrameStyle(label.Box) layout.addWidget(label, 0, 2) policy = QtGui.QSizePolicy( QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) label.setSizePolicy(policy) 

small

larger

+9
source

Source: https://habr.com/ru/post/1433440/


All Articles