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

前端 未结 2 769
孤独总比滥情好
孤独总比滥情好 2020-12-20 02:31

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

相关标签:
2条回答
  • 2020-12-20 02:54

    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()
    
    0 讨论(0)
  • 2020-12-20 03:07

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

    TracParamFrame.setLayout(grid)
    
    0 讨论(0)
提交回复
热议问题