Python PyQt小部件在网格中定位

时间:2014-05-09 14:56:28

标签: python qt grid pyqt

我有一个python代码:

#class...
def setupLayout(self):
    self.resize(1000, 600)
    label1 = QtGui.QLabel(u'<font color=green><b><i>Полученные сообщения:</i></b></font>')
    label2 = QtGui.QLabel(u'<font color=green><b><i>Описание сообщений:</i></b></font>')

    self.mainList = QtGui.QListWidget()
    self.descList = QtGui.QListWidget()

    grid = QtGui.QGridLayout()
    grid.setSpacing(1)

    grid.addWidget(label1, 1, 1)
    grid.addWidget(label2, 1, 2)

    grid.addWidget(self.mainList, 2, 1, 5, 1)
    grid.addWidget(self.descList, 2, 2, 5, 1)
#other definitions....

我希望使用窗口标题缩短标签和面板之间的距离。 grid layout problem

因此,如果我使用此代码,距离是好的,但listBoxes只保留一行:

grid.addWidget(self.mainList, 2, 1, 1, 1)
grid.addWidget(self.descList, 2, 2, 1, 1)

enter image description here 那我怎么能做一个小距离?

1 个答案:

答案 0 :(得分:1)

我倾向于使用QGridLayout来表示同质内容。出于您的目的,QHBoxLayoutQVBoxLayout的组合可以更好地发挥作用。

left = QtGui.QVBoxLayout()
# This makes the label take as little space as possible
# since QLabel's sizeHint is Minimum.
left.addWidget(label1, 0)
left.addWidget(self.mainList, 1)

right = QtGui.QVBoxLayout()
right.addWidget(label2, 0)
right.addWidget(self.descList, 1)

main = QtGui.QHBoxLayout()
main.addLayout(left, 1)    # Both sides take the same amount of space.
main.addLayout(right, 1)

Demo