PyQt : How to add a grid layout inside a QGroupBox in PyQt4

10,219

Solution 1

I modified your code, by adding this statement: TracParamFrame.setLayout(hbox)

The code with this added is as:

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()
    def initUI(self):
        hbox = QtGui.QHBoxLayout()
        grid = QtGui.QGridLayout()

        #Definition des Tracing Parameters widgets
        WindowSize = QtGui.QLabel("Window size (m)")
        SampPts = QtGui.QLabel("Sampling points")
        WindowSizeEdit = QtGui.QLineEdit()
        SampPtsEdit = QtGui.QLineEdit()
        TracParamFrame = QtGui.QGroupBox(self)
        TracParamFrame.setTitle("Tracing Parameters")
        hbox.addLayout(grid)

        grid.addWidget(WindowSize,0,0)
        grid.addWidget(WindowSizeEdit,0,1)
        grid.addWidget(SampPts,1,0)
        grid.addWidget(SampPtsEdit,1,1)
        TracParamFrame.setLayout(hbox)

        #self.setLayout(hbox)


        self.setGeometry(300,300,350,300)
        self.show()

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

Solution 2

Ok forget it, I found the solution. I had to use the setLayout method of the GroupBox as follows :

TracParamFrame.setLayout(grid)
Share:
10,219
Mathieu Paurisse
Author by

Mathieu Paurisse

Updated on June 04, 2022

Comments

  • Mathieu Paurisse
    Mathieu Paurisse almost 2 years

    I am trying to create an application window with PyQt4. I want to create a window with a frame and inside that frame some widgets such as labels and text editors. I created the frame as a QGroupBox to be able to put a title on it. I know that HBox and VBox seem to be the prefered layout when you deal with frames, however, I would like to manage the positionning of the widgets inside my frame with a grid Layout, which I find easier to manage. So I tried this piece of code :

    import sys
    from PyQt4 import QtGui, QtCore
    
    class Example(QtGui.QWidget):
    
        def __init__(self):
            super(Example, self).__init__()
    
            self.initUI()
    
    def initUI(self):      
    
        hbox = QtGui.QHBoxLayout()
        grid = QtGui.QGridLayout()
    
        #Definition des Tracing Parameters widgets
        WindowSize = QtGui.QLabel("Window size (m)")
        SampPts = QtGui.QLabel("Sampling points")
        WindowSizeEdit = QtGui.QLineEdit()
        SampPtsEdit = QtGui.QLineEdit()
        TracParamFrame = QtGui.QGroupBox(self)
        TracParamFrame.setTitle("Tracing Parameters")
        hbox.addLayout(grid)
    
        grid.addWidget(WindowSize,0,0)
        grid.addWidget(WindowSizeEdit,0,1)
        grid.addWidget(SampPts,1,0)
        grid.addWidget(SampPtsEdit,1,1)
    
        self.setLayout(hbox)
    
        self.setGeometry(300,300,350,300)
        self.show()
    
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()
    

    The main idea here was to create an hbox where I put the QGroupBox and then place a grid layout inside. The problem is that in the application generated, the widgets are placed outside the frame, and in addition I get the error :

    QLayout: Attempting to add QLayout "" to Example "", which already has a layout QWidget::setLayout: Attempting to set QLayout "" on Example "", which already has a layout