PyQt5 notes (02): Nested layouts

Source: Internet
Author: User
Tags qt designer

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.