Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use spacers in Qt

Tags:

python

qt

pyqt

I have a QGroupBox with assigned to it QVBoxLayout.

groupBox = QtGui.QGroupBox()
layout = QtGui.QVBoxLayout()
groupBox.setLayout(layout )

Then there is a single QLabel assigned to the layout.

label = QtGui.QLabel()   
layout.addWidget(label)

The problem is that QLabel is positioned at the middle of the GroupBox. While I want it to be aligned with the top edge of the GroupBox. I probably need to add some sort of spacer. So the spacer would dynamically resize itself to fill the GroupBox's empty area pushing QLabel upward.

What widget and what size policy should be used as a spacer?

like image 757
alphanumeric Avatar asked Nov 19 '15 00:11

alphanumeric


2 Answers

label = QtGui.QLabel()   
layout.addWidget(label)
verticalSpacer = QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
layout.addItem(verticalSpacer)

This should work

like image 30
Achayan Avatar answered Sep 28 '22 05:09

Achayan


In pyQt5 , the QSpacerItem and QSizePolicy classes are now a part of QtWidgets so you'd want to create a vertical spacer in this manner...

verticalSpacer = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding) 
like image 138
Luxapodular Avatar answered Sep 28 '22 06:09

Luxapodular