Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QVBoxLayout: How to vertically align widgets to the top instead of the center

Tags:

qt

qlayout

In Qt, When I add widgets to my layout, they are vertically centered by default. Is there a way to "List" the widgets from top to bottom instead of centering them vertically?

like image 382
sFuller Avatar asked Apr 10 '12 02:04

sFuller


People also ask

How do I align widgets in Qt?

Right-click the relevant widget and select Layout alignment > Top.

What is QVBoxLayout?

The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.


2 Answers

use void QLayout::setAlignment ( Qt::Alignment alignment ) method to set alignment according to your choice.

like image 90
Kunal Avatar answered Sep 20 '22 02:09

Kunal


If you have a QVBoxLayout and want your fixed size widgets to be stacked at the top, you can simply append a vertical stretch at the end:

layout.addStretch() 

If you have multiple stretchers or other stretch items, you can specify an integer stretch factor argument that defines their size ratio.

See also addStretch and addSpacerItem.

Add two layout.addStretch() before and after adding the widgets to center them vertically:

    layout.addStretch()     layout.addWidget(self.message)     layout.addWidget(self.userid_field)     layout.addWidget(self.password_field)     layout.addWidget(self.loginButton)     layout.addStretch() 

Not sure whether this answers your original question, but it is the answer to the one that I had when googling and being led to this page - so it might be useful for others too.

like image 30
coldfix Avatar answered Sep 19 '22 02:09

coldfix