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();
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…