I generated a simple UI in Qt6 Designer consisting of a QGridLayout
on a QDialog
(or QMainWindow
- the problem seems to be the same).
Now I would like this layout to fill the full size of the window and to resize itself whenever the window size is changed.
How can I achieve this?
Here is my example application with the code generated from Qt Designer and a little bit of my own code to make it run:
import sys
from PySide6.QtCore import QRect, QCoreApplication, QMetaObject
from PySide6.QtWidgets import QApplication, QWidget, QGridLayout, QLabel, QLineEdit, QSizePolicy
class Ui_Dialog():
def setupUi(self, Dialog):
if not Dialog.objectName():
Dialog.setObjectName(u"Dialog")
Dialog.resize(400, 300)
self.gridLayoutWidget = QWidget(Dialog)
self.gridLayoutWidget.setObjectName(u"gridLayoutWidget")
self.gridLayoutWidget.setGeometry(QRect(10, 20, 341, 191))
self.gridLayout = QGridLayout(self.gridLayoutWidget)
self.gridLayout.setObjectName(u"gridLayout")
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.label = QLabel(self.gridLayoutWidget)
self.label.setObjectName(u"label")
self.gridLayout.addWidget(self.label, 0, 0, 1, 1)
self.label_2 = QLabel(self.gridLayoutWidget)
self.label_2.setObjectName(u"label_2")
self.gridLayout.addWidget(self.label_2, 1, 0, 1, 1)
self.lineEdit = QLineEdit(self.gridLayoutWidget)
self.lineEdit.setObjectName(u"lineEdit")
sizePolicy = QSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.lineEdit.sizePolicy().hasHeightForWidth())
self.lineEdit.setSizePolicy(sizePolicy)
self.gridLayout.addWidget(self.lineEdit, 0, 1, 1, 1)
self.lineEdit_2 = QLineEdit(self.gridLayoutWidget)
self.lineEdit_2.setObjectName(u"lineEdit_2")
sizePolicy.setHeightForWidth(self.lineEdit_2.sizePolicy().hasHeightForWidth())
self.lineEdit_2.setSizePolicy(sizePolicy)
self.gridLayout.addWidget(self.lineEdit_2, 1, 1, 1, 1)
self.gridLayout.setColumnStretch(1, 1)
self.retranslateUi(Dialog)
QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
Dialog.setWindowTitle(QCoreApplication.translate("Dialog", u"Dialog", None))
self.label.setText(QCoreApplication.translate("Dialog", u"TextLabel", None))
self.label_2.setText(QCoreApplication.translate("Dialog", u"TextLabel", None))
class MyMainWindow(QWidget, Ui_Dialog):
def __init__(self, *args, obj=None, **kwargs):
super().__init__(*args, **kwargs)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MyMainWindow()
w.show()
app.exec()
The documentation states
QGridLayout takes the space made available to it (by its parent layout or by the parentWidget()), ...
So I think the question boils down to "How do I tell the QDialog
or QMainWindow
to make all it's space available to one QLayout
?