from PySide import QtCore
from PySide import QtGui
class UI(QtGui.QDialog):
def __init__(self):
super(UI,self).__init__()
self.setWindowTitle('My UI Title')
self.create_layout()
def create_layout(self):
mainLayout = QtGui.QGridLayout()
radioButtonSetALayout = QtGui.QVBoxLayout()
radioButtonSetBLayout = QtGui.QVBoxLayout()
#radio button set A
setALabel = QtGui.QLabel('Fruit')
radioButtonA = QtGui.QRadioButton('Apple')
radioButtonB = QtGui.QRadioButton('Pear')
radioButtonSetALayout.addWidget(setALabel)
radioButtonSetALayout.addWidget(radioButtonA)
radioButtonSetALayout.addWidget(radioButtonB)
#radio button set B
setBLabel = QtGui.QLabel('Junk')
radioButtonC = QtGui.QRadioButton('Pizza')
radioButtonD = QtGui.QRadioButton('Donut')
radioButtonE = QtGui.QRadioButton('Ice Cream')
radioButtonSetBLayout.addWidget(setBLabel)
radioButtonSetBLayout.addWidget(radioButtonC)
radioButtonSetBLayout.addWidget(radioButtonD)
radioButtonSetBLayout.addWidget(radioButtonE)
#alignment
radioButtonSetALayout.setAlignment(QtCore.Qt.AlignTop)
radioButtonSetBLayout.setAlignment(QtCore.Qt.AlignTop)
#add sub layouts to main layout
mainLayout.addLayout(radioButtonSetALayout,0,0)
mainLayout.addLayout(radioButtonSetBLayout,0,1)
self.setLayout(mainLayout)
if __name__ == '__main__':
try:
ui.close()
except:
pass
ui = UI()
ui.setAttribute(QtCore.Qt.WA_DeleteOnClose)
ui.show()
I have two QVBoxLayout's side by side in a QGridLayout, each with different quantities of QRadioButtons. By default, my two sets of QRadioButtons won't align nicely since the default behavior for a QVBoxLayout is to have added widgets aligned to the center.
I thought by setting the QVBoxLayout's alignment would remedy this but it doesn't seem to change anything at all.
My other idea was to add some sort of invisible 'spacer' to the column with less QRadiobutton's to match the quantity in the other in an attempt to even them out but from what I tried it was not quite exact.
QGridLayout::addLayout
has a fourth (optional) parameteralignment
.So instead of setting the alignment of the
QVBoxLayout
s, set it when adding the layouts.