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()
我在 QGridLayout 中有两个并排的 QVBoxLayout,每个都有不同数量的 QRadioButton。默认情况下,我的两组 QRadioButtons 不会很好地对齐,因为 QVBoxLayout 的默认行为是添加对齐到中心的小部件。
我认为通过设置 QVBoxLayout 的对齐方式可以解决这个问题,但它似乎并没有改变任何东西。
我的另一个想法是在具有较少 QRadiobutton 的列中添加某种不可见的“间隔器”,以匹配另一个中的数量,以尝试将它们均匀化,但从我尝试的结果来看,它并不完全准确。