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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…