3

下面的代码创建了一个与GroupBox()对话框同步调整大小的对话框。这是 GroupBox 的默认大小调整策略。如何更改此策略以使其GroupBox占用尽可能少的空间(区域)并且永远不会垂直扩展。如何实现?

在此处输入图像描述

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

panel=QtGui.QDialog()
panel.setLayout(QtGui.QVBoxLayout())

groupbox = QtGui.QGroupBox('Group Box')
groupbox.setLayout(QtGui.QVBoxLayout())
panel.layout().addWidget(groupbox)

for i in range(3):
    row = QtGui.QHBoxLayout()
    row.addWidget(QtGui.QLabel('Label'))
    row.addWidget(QtGui.QLineEdit())
    row.addWidget(QtGui.QPushButton('Push Button'))
    groupbox.layout().addLayout(row)

panel.layout().addWidget(QtGui.QPushButton('Confirm'))

panel.show()
app.exec_()
4

1 回答 1

3

ekhumoro 最简单的一个:

在此处输入图像描述

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

panel=QtGui.QDialog()
panel.setLayout(QtGui.QVBoxLayout())

groupbox = QtGui.QGroupBox('Group Box')
groupbox.setLayout(QtGui.QVBoxLayout())
panel.layout().addWidget(groupbox)
panel.layout().addStretch()

for i in range(3):
    row = QtGui.QHBoxLayout()
    row.addWidget(QtGui.QLabel('Label'))
    row.addWidget(QtGui.QLineEdit())
    row.addWidget(QtGui.QPushButton('Push Button'))
    groupbox.layout().addLayout(row)

panel.layout().addWidget(QtGui.QPushButton('Confirm'))

panel.show()
app.exec_()

第二种方法在我们替换为 后也.addWidget有效.addItem

from PyQt4 import QtCore, QtGui
app = QtGui.QApplication([])

panel=QtGui.QDialog()
panel.setLayout(QtGui.QVBoxLayout())

groupbox = QtGui.QGroupBox('Group Box')
groupbox.setLayout(QtGui.QVBoxLayout())
panel.layout().addWidget(groupbox)
panel.layout().addItem(QtGui.QSpacerItem(20, 40, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding))
for i in range(3):
    row = QtGui.QHBoxLayout()
    row.addWidget(QtGui.QLabel('Label'))
    row.addWidget(QtGui.QLineEdit())
    row.addWidget(QtGui.QPushButton('Push Button'))
    groupbox.layout().addLayout(row)

panel.layout().addWidget(QtGui.QPushButton('Confirm'))

panel.show()
app.exec_()
于 2017-01-25T04:29:58.107 回答