当调整大小时,如何使lineedit
坚持到顶部并button
到对话框的下边缘?
from PyQt5.QtWidgets import *
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QVBoxLayout())
layout = QHBoxLayout()
self.layout().insertLayout(0, layout)
lineedit = QLineEdit(self)
layout.addWidget(lineedit)
button = QPushButton(self)
button.setText('ok')
self.layout().addWidget(button)
self.show()
app = QApplication(list())
dialog = Dialog()
app.exec_()
答案 0 :(得分:2)
在布局中添加垂直间隔:
from PyQt5.QtWidgets import *
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QVBoxLayout())
layout = QHBoxLayout()
self.layout().insertLayout(0, layout)
lineedit = QLineEdit(self)
layout.addWidget(lineedit)
# version (1): add vertical, expanding spacer item
self.layout().addItem(
QSpacerItem(0, 0, QSizePolicy.Minimum, QSizePolicy.Expanding))
# version (2): use "addStretch"
# self.layout().addStretch()
button = QPushButton(self)
button.setText('ok')
self.layout().addWidget(button)
self.show()
app = QApplication(list())
dialog = Dialog()
app.exec_()
QSpacerItem
只是一个没有样式的空白空间,根据其大小政策在布局中消耗水平和垂直空间。
QSpacerItem
的参数:
w
- 首选宽度0
h
- 首选身高,0
hPolicy
- 横向尺寸政策,QSizePolicy.Minimum
=>首选宽度足够,项目不会水平扩展vPolicy
- 纵向尺寸政策,QSizePolicy.Expanding
=>该项目可以利用额外的空间,因此将垂直扩展并占用所有可用空间有关详细信息,请参阅https://doc.qt.io/qt-5/qspaceritem.html#QSpacerItem和http://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum。