As in the previous note, we will discuss only two layers of nested layouts.
The previous layout has one drawback: there are three inner layouts, and three empty parts are required. What if there are 10 interior layouts? Obviously it makes people uncomfortable.
Just when I was playing Qt Designer, I found a better way to do it, no matter how many inner layouts you have, you just need an empty part .
First, Process analysis:
1. Prepare a global part to "host" the Global layout
# Global Part (note the parameter self) for the "hosting" Global layout WWG = Qwidget (self)
2. Define the global layout again
# Global Layout (note parameter WWG)
3. Definition of three local layouts
# three local layouts HL = qhboxlayout () = Qgridlayout ()
4. Of course, the partial layout must be intended to place some parts
Pass # this adds parts to the local layout
5. Add three local layouts to a global layout
# add to Global layout wl.addlayout (GL) wl.addlayout (HL)
Second, all the code
fromPyqt5.qtwidgetsImport*classMywindow (qwidget):def __init__(self): Super ().__init__() Self.setwindowtitle ('PyQt5 Layout Example') Self.resize (400, 300) #Global Part (note the parameter self) for the "hosting" Global layoutWWG =Qwidget (self) WL= Qvboxlayout (WWG)#Global Layout (note parameter WWG)VL= Qvboxlayout ()#three local layoutsHL =qhboxlayout () GL=qgridlayout ()Pass #this adds parts to the local layoutWl.addlayout (VL)#add to Global layoutWl.addlayout (GL) wl.addlayout (HL)if __name__=="__main__": Importsys app=Qapplication (SYS.ARGV) win=Mywindow () win.show () Sys.exit (App.exec_ ())
Add:
According to the above thinking, further, you can not spare parts of the empty:
fromPyqt5.qtwidgetsImport*classMywindow (qwidget):def __init__(self):super ().__init__() Self.setwindowtitle ('PyQt5 Layout Example') Self.resize (400, 300) #Global Layout (note parameter self)WL =qvboxlayout (self)#Local LayoutVL =qvboxlayout () HL=qhboxlayout () GL=qgridlayout ()#this adds parts to the local layoutHl.addwidget (Qpushbutton ('1')) Hl.addwidget (Qpushbutton ('2')) Vl.addwidget (Qpushbutton ('3')) Vl.addwidget (Qpushbutton ('4')) Vl.addwidget (Qpushbutton ('5')) Gl.addwidget (Qpushbutton ('6'), 0,0) Gl.addwidget (Qpushbutton ('7'), 0,1) Gl.addwidget (Qpushbutton ('8'), 1, 0) gl.addwidget (Qpushbutton ('9'),) #add to Global layoutwl.addlayout (HL) wl.addlayout (VL) Wl.addlayout (GL)if __name__=="__main__": Importsys app=Qapplication (SYS.ARGV) win=Mywindow () win.show () Sys.exit (App.exec_ ())
Add:
Addlayout () method prototype:
addlayout (qlayout, Row, col, Row_cross, Col_cross, qt.alignment)
AddWidget () method prototype:
AddWidget (Qwidget, Row, col, Row_cross, Col_cross, qt.alignment)
PyQt5 notes (02): Nested layouts