PDA

View Full Version : Make QVBoxLayout act like QFormLayout



petwoip
7th March 2011, 21:30
I've been trying to make a QVBoxLayout act like a QFormLayout so that when I add widgets to the QVBoxLayout, they do not stretch across the length of the parent widget. I've been trying to do this with sizePolicies, sretchFactors, etc, but I can't seem to figure it out.

Here's my code:


QPushButton* thing1 = new QPushButton(QObject::tr("Hi"));
QPushButton* thing2 = new QPushButton(QObject::tr("There"));
QPushButton* thing3 = new QPushButton(QObject::tr("Again"));

QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(thing1);
layout->addWidget(thing2);
layout->addWidget(thing3);

QWidget* widget = new QWidget();
widget->resize(300,300);
widget->setLayout(layout);
widget->show();


Output using a QFormLayout:

http://i.imgur.com/nWI28.png

Output using a QVBoxLayout:

http://i.imgur.com/lq0oX.png

I'd like the QVBoxLayout version to mimic the QFormLayout version.

wysota
8th March 2011, 12:17
Why don't you just use QFormLayout then?

petwoip
8th March 2011, 19:55
I suppose I could in some cases, but what if I wanted to use a QHBoxLayout instead of a QVBoxLayout (to my [limited] knowledge there is no way to make a sideways QFormLayout). I'm trying to understand what property of layouts lets me modify the layout's behavior when i expand the window.

norobro
8th March 2011, 21:03
Maybe I'm missing something but won't QVBoxLayout::addStretch() do what you want?

petwoip
8th March 2011, 21:53
Oh cool, addStretch(0) achieves the desired behavior!

wysota
9th March 2011, 11:54
I suppose I could in some cases, but what if I wanted to use a QHBoxLayout instead of a QVBoxLayout (to my [limited] knowledge there is no way to make a sideways QFormLayout). I'm trying to understand what property of layouts lets me modify the layout's behavior when i expand the window.

The layout behaviour is decided upon settings of the widgets (their size hint and size policies). Your problem is not a problem with QHBoxLayout as it will do what you want out of the box.

ChiliPalmer
9th March 2011, 20:51
setAlignment(Qt::AlignTop); should also do the trick

petwoip
9th March 2011, 21:13
Thanks for the help guys! This is a lot clearer to me now.