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

QtQlayout

Qt Problem Overview


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?

Qt Solutions


Solution 1 - Qt

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

Solution 2 - Qt

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.

Solution 3 - Qt

I find this a little more complicated than just using layout.setAlignment(). It kept not working for me until just now, when I figured out that if you have expanding widgets that you set a maximum height for, then that widget will not be aligned the way you want.

Here is example code that does not top align the QTextBrowser() widget even though I call layout.setAlignment(Qt.AlignTop). Sorry that it is in Python, but it is pretty easy to translate to C++ (I have gone the other way many times).

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWidget(QWidget):
    """
    Create a widget that aligns its contents to the top.
    """
    
    def __init__(self, parent=None):
        
        QWidget.__init__(self, parent)
        
        layout = QVBoxLayout()
        
        label = QLabel('label:')
        layout.addWidget(label)

        info = QTextBrowser(self)
        info.setMinimumHeight(100)
        info.setMaximumHeight(200)
        layout.addWidget(info)        
        # Uncomment the next line to get this to align top.
#         layout.setAlignment(info, Qt.AlignTop)
        
        # Create a progress bar layout.
        button = QPushButton('Button 1')        
        layout.addWidget(button)        

        # This will align all the widgets to the top except
        # for the QTextBrowser() since it has a maximum size set.
        layout.setAlignment(Qt.AlignTop)
        
        self.setLayout(layout)


if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)

    widget = MyWidget()
    widget.show()
    widget.resize(QSize(900, 400))
    
    app.exec_()

The following explicitly calls layout.setAlignment(info, Qt.AlignTop) to get the expending text widget to work.

from PyQt4.QtCore import *
from PyQt4.QtGui import *

class MyWidget(QWidget):
    """
    Create a widget that aligns its contents to the top.
    """
    
    def __init__(self, parent=None):
        
        QWidget.__init__(self, parent)
        
        layout = QVBoxLayout()
        
        label = QLabel('label:')
        layout.addWidget(label)

        info = QTextBrowser(self)
        info.setMinimumHeight(100)
        info.setMaximumHeight(200)
        layout.addWidget(info)        
        # Uncomment the next line to get this to align top.
        layout.setAlignment(info, Qt.AlignTop)
        
        # Create a progress bar layout.
        button = QPushButton('Button 1')        
        layout.addWidget(button)        

        # This will align all the widgets to the top except
        # for the QTextBrowser() since it has a maximum size set.
        layout.setAlignment(Qt.AlignTop)
        
        self.setLayout(layout)


if __name__ == '__main__':

    import sys

    app = QApplication(sys.argv)

    widget = MyWidget()
    widget.show()
    widget.resize(QSize(900, 400))
    
    app.exec_()

Solution 4 - Qt

After comparison between the two solutions, it seems that :

myLayout.setAlignment(Qt.AlignTop)

works for several widget alignement but :

myLayout.setAlignment(myWidget, Qt.AlignTop)

works only for the first widget you add to the layout. After all, the solution depends also to the QSizePolicy of yours widgets.

Solution 5 - Qt

If you are using QT creator, you just add a "Vertical Spacers" at the bottom of your widget.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsFullerView Question on Stackoverflow
Solution 1 - QtKunalView Answer on Stackoverflow
Solution 2 - QtcoldfixView Answer on Stackoverflow
Solution 3 - QtnoisygeckoView Answer on Stackoverflow
Solution 4 - QtStarfightView Answer on Stackoverflow
Solution 5 - QtJOEView Answer on Stackoverflow