Qt : Qwidget size

Seub picture Seub · Sep 10, 2012 · Viewed 16.1k times · Source

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

screen capture

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

I am having a really hard time trying to figure out how the sizes work. So far, I have just defined a minimum size for the first column's width (in my window's layout) to fix the menu's width, and I have set a fixed size for Canvas in its constructor (like this: setFixedSize(QSize(size_in_pixels, size_in_pixels));).

The problem is of course that this behaves badly under rescaling the window by the user. I guess what I would like to do is sort of set my canvas's sizeHint to size_in_pixels (my preferred size) but this hardly seems possible. I would also like my canvases to have same height and width at all times. I have been reading Qt documentation and trying several things but I can't come up with a solution.

What would be the way to go according to you? Thanks a lot for your insights.

Answer

jdi picture jdi · Sep 10, 2012

You can just set the canvas widgets to an Expanding size policy. They will equally consume the remaining space as the layout increases in size.

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