Search code examples
pythonpyqt5qmainwindow

Central widget position of layout


Is there a way to postion or resize layout in central widget of qmain window.

I am trying to write an app with QMainWindow. I am setting different Layout in central widget but the spacing of those widgets are completely different

class MainApp(QMainWindow):

    def __init__(self,parent = None):
        super().__init__(parent)
    
        self.statusBar().showMessage('Select any option to proceed',3000)
    
        self.setWindowTitle("CH APTMO App")
        self.setGeometry(100,100,1050,600)
        self.setFixedSize(self.size())
    
        self.central_widget = QStackedWidget()
        self.setCentralWidget(self.central_widget)


class InsideWidget(QWidget):
    def __init__(self,parent=None):
        super().__init__(parent)
        self.From = QLabel("From")
        self.To = QLabel("To")
        self.Type = QLabel("Type")
        self.Number = QLabel("Number")
      
        from_list = ["val1","val2","val3"]
        self.fromentry = QComboBox()
        self.fromentry.setEditable(True)
        self.fromentry.addItems(from_list)
        self.toentry = QLineEdit()
        self.typeentry = QLineEdit()
        self.numentry = QLineEdit()
       
        self.receivebaglayout = QGridLayout()
       
        self.receivebaglayout.addWidget(self.From,0,0)
        self.receivebaglayout.addWidget(self.To,0,1)
        self.receivebaglayout.addWidget(self.Type,0,2)
        self.receivebaglayout.addWidget(self.Number,0,3)
    
        self.receivebaglayout.addWidget(self.fromentry,1,0)
        self.receivebaglayout.addWidget(self.toentry,1,1)
        self.receivebaglayout.addWidget(self.typeentry,1,2)
        self.receivebaglayout.addWidget(self.numentry,1,3)
    
        self.receivebaglayout.setSpacing(10)
    
        self.setLayout(self.receivebaglayout)

The layout is messed up the grid layout in the central widget is like this

Picture showing the spacing between row 1 and 2:
picture showing the spacing between row 1 and 2

If we add many widgets to the grid layout its been adjusted properly. But when having less widgets it is spreading all over the centralwidget layout.

How could we stop this?

Output expected is added

The output expected:
The output expected


Solution

  • By default Qt uses the sizePolicy to calculate the size of each row of the QGridLayout. In this case, the QLabel tends to stretch vertically and the QLineEdit and QComboBox do not see this behavior.

    Since it is not clear what the OP wants I will offer several solutions depending on the case:

    • If you want the items to be positioned towards the top then just add a stretch at the end of the row:

      self.receivebaglayout.setRowStretch(2, 1)
      

    enter image description here

    • If you want the items to be positioned towards the middle then just add a stretch at the beginning and end of the row so the first row should be the second:

      self.receivebaglayout = QGridLayout(self)
      
      self.receivebaglayout.setRowStretch(0, 1)
      
      self.receivebaglayout.addWidget(self.From, 1, 0)
      self.receivebaglayout.addWidget(self.To, 1, 1)
      self.receivebaglayout.addWidget(self.Type, 1, 2)
      self.receivebaglayout.addWidget(self.Number, 1, 3)
      
      self.receivebaglayout.addWidget(self.fromentry, 2, 0)
      self.receivebaglayout.addWidget(self.toentry, 2, 1)
      self.receivebaglayout.addWidget(self.typeentry, 2, 2)
      self.receivebaglayout.addWidget(self.numentry, 2, 3)
      
      self.receivebaglayout.setRowStretch(3, 1)
      
      self.receivebaglayout.setSpacing(10)
      

    enter image description here