QVBoxLayout 的 'setAlignment' 方法未按预期工作
QVBoxLayout's 'setAlignment' method not working as expected
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 较少的列中添加某种不可见的 'spacer' 以匹配另一个列中的数量,试图将它们拉平,但根据我的尝试,它并不十分准确.
QGridLayout::addLayout
有第四个(可选)参数 alignment
.
The alignment is specified by alignment
. The default alignment is 0, which means that the widget fills the entire cell.
因此,不要设置 QVBoxLayout
的对齐方式,而是在添加布局时进行设置。
def create_layout(self):
....
mainLayout.addLayout(radioButtonSetALayout,0,0,QtCore.Qt.AlignTop)
mainLayout.addLayout(radioButtonSetBLayout,0,1,QtCore.Qt.AlignTop)
self.setLayout(mainLayout)
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 较少的列中添加某种不可见的 'spacer' 以匹配另一个列中的数量,试图将它们拉平,但根据我的尝试,它并不十分准确.
QGridLayout::addLayout
有第四个(可选)参数 alignment
.
The alignment is specified by
alignment
. The default alignment is 0, which means that the widget fills the entire cell.
因此,不要设置 QVBoxLayout
的对齐方式,而是在添加布局时进行设置。
def create_layout(self):
....
mainLayout.addLayout(radioButtonSetALayout,0,0,QtCore.Qt.AlignTop)
mainLayout.addLayout(radioButtonSetBLayout,0,1,QtCore.Qt.AlignTop)
self.setLayout(mainLayout)