pythonqtpyqtpyqt5qt-designer

Can't have Designer to resize Qwidget inside QGridLayout in pyqt5?


I can run this code:

import sys

from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QGridLayout, QVBoxLayout, QLabel


from PyQt5 import QtGui, QtCore

from PyQt5.QtGui import QDrag



class MainWidget(QWidget):
    
    def __init__(self, *args, **kwargs):
        
        super().__init__()
        
        
        
        self.setStyleSheet("border: 1px solid black;") 
        
        
        self.layout = QGridLayout(self)
        
        self.resize(600, 400)
        
        
        # for i in [(0,0) , (0,1) , (1,0) , (1,1)]:
            
        for i in [(0,0)]:
            
            
            
            print(str(i))
            
            wig = QWidget()
            
            wig.resize(150,100)
            
            
            
            
            # VBox = QVBoxLayout()
            
            # VBox.addStretch(1)
            
            
            # VBox.addWidget(label)
            
            # wig.setLayout(VBox)
            
            
            scroll = QScrollArea(wig)
            
            scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
            

            scrollAreaWidgetContents = QWidget()
            

            # label = QLabel()
            
            # font = self.font()
            
            
            # font.setPointSize(15)
            
            # label.setFont(font)
            
            # label.setText(str(i)+'   ')
            
            # VBox = QVBoxLayout()
            
            # VBox.addWidget(label)
            
            # scrollAreaWidgetContents.setLayout(VBox)
            
            scroll.setWidget(scrollAreaWidgetContents)
            
            
            self.layout.addWidget(scroll, i[0] , i[1])
            
        
        
        self.show()
    
    

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    sys.exit(app.exec_())

and be able to resize my widget together with the widgets contained in the QGridlayout.

While using PyQt-Designer to write my app (test003.ui), after converting via pyuic5 I get:

test003.py:

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(658, 538)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(1)
        sizePolicy.setVerticalStretch(1)
        sizePolicy.setHeightForWidth(Form.sizePolicy().hasHeightForWidth())
        Form.setSizePolicy(sizePolicy)
        Form.setSizeIncrement(QtCore.QSize(1, 1))
        Form.setStyleSheet("QWidget{background-color: yellow\n"
"}")
        self.gridLayoutWidget = QtWidgets.QWidget(Form)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 10, 641, 501))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setSizeConstraint(QtWidgets.QLayout.SetNoConstraint)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.scrollArea = QtWidgets.QScrollArea(self.gridLayoutWidget)
        self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setObjectName("scrollArea")
        self.scrollAreaWidgetContents_2 = QtWidgets.QWidget()
        self.scrollAreaWidgetContents_2.setGeometry(QtCore.QRect(0, 0, 614, 495))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents_2.sizePolicy().hasHeightForWidth())
        self.scrollAreaWidgetContents_2.setSizePolicy(sizePolicy)
        self.scrollAreaWidgetContents_2.setStyleSheet("QWidget{background-color: blue}")
        self.scrollAreaWidgetContents_2.setObjectName("scrollAreaWidgetContents_2")
        self.scrollArea.setWidget(self.scrollAreaWidgetContents_2)
        self.gridLayout.addWidget(self.scrollArea, 0, 0, 1, 1)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))

and with main code:

import sys

from PyQt5.QtWidgets import QApplication, QScrollArea, QWidget, QGridLayout, QVBoxLayout


from PyQt5 import QtGui

from PyQt5.QtGui import QDrag


# from untitled001 import Ui_Form

# from untitled002 import Ui_Form

# from untitled003b import Ui_Form

# from test001 import Ui_Form # form


# from test002 import Ui_Form #form + grid

from test003 import Ui_Form #form + grid + widget


class MainWidget(QWidget, Ui_Form):
    
    def __init__(self, *args, **kwargs):
        
        super().__init__()
        
        self.setupUi(self)
        
        
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = MainWidget()
    
    # w.show()
    
    sys.exit(app.exec_())

but I get:

enter image description here

where the blue widget doesn't resize together with main one:

enter image description here

What am I doing wrong? I wasn't able to find any hint in PyQt designer and reading out the test003.py ui.


Solution

  • found an answer here : pyqt5 : Unable to maximize widgets on Window resize

    quote of one of the comments:

    You're only setting layouts for the "layout widgets" you're creating, but those widget are not managed by a parent layout, which is what should be set on the central widget. Right click on an empty area outside any other widget, and select the appropriate layout from the "Lay out" submenu. –

    here Designer working solution for example of my app:

    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Ui_Form(object):
        def setupUi(self, Form):
            Form.setObjectName("Form")
            Form.resize(704, 500)
            Form.setStyleSheet("")
            self.gridLayout = QtWidgets.QGridLayout(Form)
            self.gridLayout.setObjectName("gridLayout")
            self.scrollArea_1 = QtWidgets.QScrollArea(Form)
            self.scrollArea_1.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
            self.scrollArea_1.setWidgetResizable(True)
            self.scrollArea_1.setObjectName("scrollArea_1")
            self.scrollAreaWidgetContents = QtWidgets.QWidget()
            self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 667, 484))
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.scrollAreaWidgetContents.sizePolicy().hasHeightForWidth())
            self.scrollAreaWidgetContents.setSizePolicy(sizePolicy)
            self.scrollAreaWidgetContents.setStyleSheet("")
            self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
            self.horizontalLayout_2 = QtWidgets.QHBoxLayout(self.scrollAreaWidgetContents)
            self.horizontalLayout_2.setObjectName("horizontalLayout_2")
            self.widget = QtWidgets.QWidget(self.scrollAreaWidgetContents)
            sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding)
            sizePolicy.setHorizontalStretch(0)
            sizePolicy.setVerticalStretch(0)
            sizePolicy.setHeightForWidth(self.widget.sizePolicy().hasHeightForWidth())
            self.widget.setSizePolicy(sizePolicy)
            self.widget.setStyleSheet("QWidget{border: 3px solid red}\n"
    "\n"
    "QWidget{background-color: green}")
            self.widget.setObjectName("widget")
            self.horizontalLayout_2.addWidget(self.widget)
            self.scrollArea_1.setWidget(self.scrollAreaWidgetContents)
            self.gridLayout.addWidget(self.scrollArea_1, 1, 1, 1, 1)
    
            self.retranslateUi(Form)
            QtCore.QMetaObject.connectSlotsByName(Form)
    
        def retranslateUi(self, Form):
            _translate = QtCore.QCoreApplication.translate
            Form.setWindowTitle(_translate("Form", "Form"))
    

    pic:

    enter image description here