pythonpyqt5qlabelqtexteditqgridlayout

Pyqt5 QGridlayout issue when using QTextEdit


I want to adjust height and width using grid layout.

class MatchStepWidget(QWidget):
    def __init__(self,parent):

        super(MatchStepWidget,self).__init__(parent)
        self.initUI()

    def initUI(self):

        layout = QGridLayout(self)
        
        test = QTextEdit(self)
        bt_test = QPushButton(self)

        layout.addWidget(test,0,0,1,1)
        layout.addWidget(bt_test,1,0,1,1)
        # bt_test.hide()
        pass
if __name__ == "__main__":
    app = QApplication(sys.argv)
    myapp = MatchStepWidget(None)
    myapp.show()
    sys.exit(app.exec_())

I set row span and col span as same for both button and textedit. but Textedit covers so much area than pushbutton How can i adjust Textedit area using grid layout?


Solution

  • The default QPushButton will not stretch vertically, if you want to change it then you must modify the QSizePolicy. Also if you want the height of the QPushButton and the QTextEdit to be the same then you must set the stretch factor for each row of the layout:

    def initUI(self):
        test = QTextEdit()
        bt_test = QPushButton()
    
        layout = QGridLayout(self)
        layout.addWidget(test, 0, 0)
        layout.addWidget(bt_test, 1, 0)
    
        sp = bt_test.sizePolicy()
        sp.setVerticalPolicy(QSizePolicy.Minimum)
        bt_test.setSizePolicy(sp)
    
        layout.setRowStretch(0, 1)
        layout.setRowStretch(1, 1)