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?
Right-click the relevant widget and select Layout alignment > Top.
The QVBoxLayout class lines up widgets vertically. This class is used to construct vertical box layout objects. See QBoxLayout for details.
use void QLayout::setAlignment ( Qt::Alignment alignment )
method to set alignment according to your choice.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With