The most common layout category is the grid layout (qgridlayout). This layout divides the window space into a number of rows and columns. To create this layout, we need to use the Qgridlayout class.
#!/usr/bin/python#-*-coding:utf-8-*-ImportSYS fromPyQt4ImportQtguiclassGridLayout (qtgui.qwidget):def __init__(Self, parent =None): Qtgui.qwidget.__init__(self) self.setwindowtitle ('Grid Layout') Names= ['Cls','Bck',"','Close','7','8','9','/', '4','5','6','*','1','2','3', '-','0','.','=','+'] Grid=qtgui.qgridlayout () Pos= [(x, Y) forXinchRange (5) forYinchRange (4)] forIinchRange (len (POS)): Button=Qtgui.qpushbutton (names[i])ifi = = 2: Grid.addwidget (Qtgui.qlabel ("'), 0, 2) Else: Grid.addwidget (Button, pos[i][0], pos[i][1]) self.setlayout (grid) app=qtgui.qapplication (sys.argv) box=GridLayout () box.show () Sys.exit (App.exec_ ())
Effect:
In this example, we create a set of buttons that are grid-layout-based. To fill the gap between the bck and close buttons, we use the Qlabel part.
Grid= Qtgui.qgridlayout ()
The statement creates a grid layout.
if i = = 2:
Grid.addwidget (Qtgui.qlabel ("), 0, 2)
Else
Grid.addwidget (Button, pos[i][0], pos[i][1])
Using the AddWidget () method, we add parts to the grid layout. The parameters of the AddWidget () method are, in turn, the part, row, and column number to be added to the local.
Parts can span multiple rows or columns in a grid layout. We will demonstrate the situation in the following example.
#!/usr/bin/python#-*-coding:utf-8-*-ImportSYS fromPyQt4ImportQtguiclassGridLayout (qtgui.qwidget):def __init__(Self, parent =None): Qtgui.qwidget.__init__(self) self.setwindowtitle ('Grid Layout') Title= Qtgui.qlabel ('Title') Author= Qtgui.qlabel ('Author') Review= Qtgui.qlabel ('Review') Titleedit=qtgui.qlineedit () Authoredit=qtgui.qlineedit () Reviewedit=qtgui.qlineedit () grid=qtgui.qgridlayout () grid.setspacing (10) Grid.addwidget (title,1, 0) grid.addwidget (titleedit,1, 1) Grid.addwidget (author,2, 0) grid.addwidget (authoredit,2, 1) Grid.addwidget (review,3, 0) grid.addwidget (reviewedit,3, 1, 5, 1) self.setlayout (GRID) Self.resize (350, 300) App=qtgui.qapplication (sys.argv) box=GridLayout () box.show () Sys.exit (App.exec_ ())
Effect:
Grid = Qtgui.qgridlayout ()
Grid.setspacing (10)
With these two sentences, we created a layout manager and set the part interval in the layout manager to 10 kerning (the peer's horizontal interval).
Grid.addwidget (Reviewedit, 3, 1, 5, 1)
We can set the row and column spans for the part that joins the grid layout, and in the above statement we set the line span of the Reviewedit part to 5 and the span to 1.
PyQt4 Grid Layout