0

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?

silmaril
  • 1
  • 2
  • Don't add the layout from the widget box, those are intended for nested layouts: remove that (right click on its content and choose "break layout") and then just select a valid layout from the "Lay out" submenu that appears when you right click on an empty area of the window. – musicamante Jan 17 '23 at 11:08
  • @musicamante: Yes, this works! Unfortunately I cannot set a comment as the solution - but thanks anyway :-) – silmaril Jan 17 '23 at 15:59

1 Answers1

0

You need to set the layout on QDialog for it to occupy the entire space. Add the following to your Ui_Dialog() setupUi method to get this behavior.

self.gridLayout.setContentsMargins(5, 5, 5, 5)
self.setLayout(self.gridLayout)

And the output is:

enter image description here

Aamir
  • 1,974
  • 1
  • 14
  • 18
  • Thanks @Aamir! Calling setLayout() has the desired effect and with this I also found the [documentation](https://doc.qt.io/qt-6/designer-layouts.html#setting-a-top-level-layout) on how to do it "the right way" in Qt Designer. – silmaril Jan 17 '23 at 15:49
  • 1
    @silmaril Just remember that you're not expected to edit pyuic generated scripts: if you created the UI in designer, set the layout from there. – musicamante Jan 17 '23 at 18:57