12

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?

alphanumeric
  • 17,967
  • 64
  • 244
  • 392

2 Answers2

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

This should work

Mohd
  • 5,523
  • 7
  • 19
  • 30
Achayan
  • 5,720
  • 2
  • 39
  • 61
15

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) 
Luxapodular
  • 159
  • 1
  • 4