I would like to force an expanding space in my QFormLayout, but no matter what QFormLayout only uses the QSpaceItem::sizeHint(). Does anyone know a way around this, or the proper way to handle this?
MyWidget::MyWidget(QWidget *parent) : QWidget(parent)
{
SetupLayout();
}
void MyWidget::SetupLayout()
{
QFormLayout * layout = new QFormLayout();
layout->addRow("Something1", new QComboBox());
layout->addRow("Something2", new QSpinBox());
//Spacer
layout->addItem(new QSpacerItem(0,10, QSizePolicy::Expanding, QSizePolicy::Expanding));
layout->addRow(QPushButton("Something3"));
setLayout(layout);
}
So there were a few different issues:
QFormLayoutdo not expand like other layouts. My widgets (a few of them) were being placed into aQFormLayout. This prevented them from expanding. I switched my main parent layout fromQFormLayouttoQVBoxLayout. This made me have to useQLayout::setAlignment(Qt::AlignTop)QVBoxLayout. The widget above uses aQFormLayout. To get this expand, I had to use the following line in myQSpacerItem:QSpacerItem * my_spacer = new QSpacerItem(0,1000, QSizePolicy::Expanding, QSizePolicy::Expanding);I am supplying some example code. The goal is to show the hierarchy, and where QFormLayout would cause trouble.
Example code:
If
MyWidgetuses aQFormLayout, things are not expanded unless I add spacers with size hints. However, ifMyWidgetuses aQVBoxLayout, anyQWidgetsinside it are expanded correctly.