Issue
How to make lineedit
stick to the top and button
to the lower edge of the dialog when it gets resized?
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_()
Solution
Add a vertical spacer to the layout:
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_()
A QSpacerItem
is just an unstyled, blank space, that consumes horizontal and vertical space in the layout according to its size policies.
QSpacerItem
's parameters:
w
- preferred width,0
h
- preferred height,0
hPolicy
- horizontal size policy,QSizePolicy.Minimum
=> the preferred width is sufficient, the item will not expand horizontallyvPolicy
- vertical size policy,QSizePolicy.Expanding
=> the item can make use of extra space, and thus will expand vertically and take all available space
See https://doc.qt.io/qt-5/qspaceritem.html#QSpacerItem and http://doc.qt.io/qt-5/qsizepolicy.html#Policy-enum for details.
Answered By - Flopp
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.