Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
928 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...