Reputation: 33385
I have a widget with two stacked layers. The first looks like this, which is fine:
The second looks like this, which is wrong.
Here is complete code which produces the widget and prints the size policies.
#include <QtWidgets>
int main (int argc, char ** argv)
{
QApplication app (argc, argv);
QWidget w;
auto stack = new QStackedWidget ();
QWidget * labelbox = new QWidget ();
QWidget * combobox = new QComboBox ();
QWidget * label = new QLabel ("Label");
QWidget * button = new QPushButton ("Button");
QWidget * date = new QDateTimeEdit ();
auto gl = new QGridLayout (& w);
auto hl = new QHBoxLayout (labelbox);
hl -> addWidget (label);
hl -> addWidget (button);
stack -> addWidget (labelbox);
stack -> addWidget (combobox);
stack -> setCurrentIndex (1); // 0 is the good layer, 1 is the bad
gl -> addWidget (date);
gl -> addWidget (stack, 0, 1);
w .setMinimumSize (300, 200);
w .show ();
for (auto x : {(QWidget *) stack, labelbox, combobox, label, button, date})
{
qWarning ()
<< x -> metaObject () -> className ()
<< x -> sizePolicy ();
}
return app .exec ();
}
The stdout is as follows:
QStackedWidget QSizePolicy(horizontalPolicy = 5, verticalPolicy = 5)
QWidget QSizePolicy(horizontalPolicy = 5, verticalPolicy = 5)
QComboBox QSizePolicy(horizontalPolicy = 5, verticalPolicy = 0)
QLabel QSizePolicy(horizontalPolicy = 1, verticalPolicy = 5)
QPushButton QSizePolicy(horizontalPolicy = 1, verticalPolicy = 0)
QDateTimeEdit QSizePolicy(horizontalPolicy = 1, verticalPolicy = 0)
The documentation for these values is:
QSizePolicy::Fixed = 0 The QWidget::sizeHint() is the only acceptable alternative, so the widget can never grow or shrink (e.g. the vertical direction of a push button).
QSizePolicy::GrowFlag = 1 The widget can grow beyond its size hint if necessary.
QSizePolicy::ExpandFlag = 2 The widget should get as much space as possible.
QSizePolicy::ShrinkFlag = 4 The widget can shrink below its size hint if necessary.
QSizePolicy::IgnoreFlag = 8 The widget's size hint is ignored. The widget will get as much space as possible.
So it looks like the vertical size policies are Fixed
except for the QStackedWidget
, the QWidget
which contains the label and button, and the QLabel
, which all have GrowFlag|ShrinkFlag
.
Why does the QComboBox
grow vertically, despite its Fixed
policy?
Why does this not happen to the QPushButton
or the QDateTimeEdit
?
This happens with Qt 4.8 and Qt 5.1 on Ubuntu 15.04.
Upvotes: 2
Views: 1312
Reputation: 1298
You are assigning the combo box directly to the QStackedWidget but haven't added it to a QLayout first. Assign it to a layout, add this layout to a QWidget and then add this QWidget to the QStackedWidget.
Example:
QWidget * combowidget = new QWidget ();
QWidget * combobox = new QComboBox ();
auto vl = new QVBoxLayout (combowidget);
vl->addWidget(combobox);
stack->addWidget (combowidget);
Upvotes: 3