我建议使用QVBoxLayout作为主布局,在QFormLayout和按钮的QHBoxLayout之间进行拉伸。
基于当前对话框的示例:
import sys
from PyQt4 import QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
label = QtGui.QLabel('My Label')
line_edit = QtGui.QLineEdit()
form_layout = QtGui.QFormLayout()
form_layout.addRow(label, line_edit)
close_button = QtGui.QPushButton('Close')
execute_button = QtGui.QPushButton('Execute')
button_layout = QtGui.QHBoxLayout()
button_layout.addWidget(close_button)
button_layout.addWidget(execute_button)
main_layout = QtGui.QVBoxLayout()
main_layout.addLayout(form_layout)
main_layout.addStretch()
main_layout.addLayout(button_layout)
central_widget = QtGui.QWidget()
central_widget.setLayout(main_layout)
self.setCentralWidget(central_widget)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.resize(480, 320)
window.show()
sys.exit(app.exec_())