简体   繁体   中英

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()

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) parameter alignment .

The alignment is specified by alignment . The default alignment is 0, which means that the widget fills the entire cell.

So instead of setting the alignment of the QVBoxLayout s, set it when adding the layouts.

def create_layout(self):

    ....

    mainLayout.addLayout(radioButtonSetALayout,0,0,QtCore.Qt.AlignTop)
    mainLayout.addLayout(radioButtonSetBLayout,0,1,QtCore.Qt.AlignTop)

    self.setLayout(mainLayout)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM