I have a container widget.
It's size policies are "Fixed" (both vertical and horizontal). it has setMaxmimumWidth and setMaximumHeight defined.
I put an QHBoxLayout container there. adding widgets to it (with fixed width/height too). But when i call "Show", none of this gets respected. The height is random almost, doesn't match any of the internal widgets in the layout, twice the size of the tallest item, and the width of my container is stretched far beyond the fixed width I set it.
What am I missing? Why can't the container stay with fixed width/height as I defined it? why is the layout interfering with it and not respecting its constraints?
QVBoxLayout organizes your widgets vertically in a window. Instead of organizing all the widgets yourself (specifying the geographic location), you can let PyQt take care of it. Every new widget you add with . addWidget() , is added vertically. Basically you get a vertical list of your widgets.
The Qt layout system provides a simple and powerful way of automatically arranging child widgets within a widget to ensure that they make good use of the available space.
The confusion comes from the fact that widgets that have fixed sizes do indeed have fixed sizes. The following holds: if a layout has only fixed-size widgets, then the layout's overall size is nominally fixed, and the widgets inside of it can't grow. If you set it as a layout on a window, it can constrain the window from growing if you set the SetFixedSize
constraint on it. It is useless to set any sort of size policies on a window with a layout that has only fixed size items - unless you want to grow the spacing between the widgets.
If you want your widgets to stay fixed size, but the spacing between them to grow, the only change necessary to the code below is to set all widgets to fixed size policy. Since they can't grow, the spacing is the only thing that can. Literally set all policies in the example to QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)
and you'll get this behavior.
If you want widgets of a certain minimum size and grow-able, and the overall container widget of a certain minimum size, then just set it so. This means:
Set the minimum, maximum or fixed size or width/height on the container widget.
Set the minimum, maximum or fixed sizes or widths/heights on the widgets.
Set the size policies on the widgets to reflect the behavior you want. You may want, for example, some of the widgets to grow in certain directions only, or not at all.
At least one widget will need to grow in a given direction if you set the minimum size on the container. Thus, if you set the width on the container, at least one laid out widget must be able to expand horizontally to fill it. If you set the height on the container, at least one laid out widget must be able to expand vertically to fill it.
The below works fine under Qt 4.8.5 and 5.1.1. The window can be expanded but not contracted - it has a minimum size. You can change the setMinimumSize
to setFixedSize
to obtain a window with fixed size, or you can set both minimum and maximum sizes.
#include <QApplication> #include <QWidget> #include <QHBoxLayout> #include <QLabel> #include <QFontDatabase> int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget window; QHBoxLayout layout(&window); QLabel lbl1("one"); lbl1.setStyleSheet("QLabel { background-color: #FF8080 }"); lbl1.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); QLabel lbl2("two"); lbl2.setStyleSheet("QLabel { background-color: #80FF80 }"); lbl2.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); lbl2.setMinimumHeight(300); QLabel lbl3("three"); lbl3.setStyleSheet("QLabel { background-color: #8080FF }"); lbl3.setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed)); lbl3.setMinimumWidth(300); layout.addWidget(&lbl1); layout.addWidget(&lbl2); layout.addWidget(&lbl3); window.setMinimumSize(800, 800); // Any combination of setMinimumSize and setMaximumSize is OK. // If the minimum and maximum are the same, just do setFixedSize window.show(); return a.exec(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With