简体   繁体   中英

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

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

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()

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

TracParamFrame.setLayout(grid)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM