PyQt5 Notes (01): Nested layouts

Source: Internet
Author: User

 

PYQT5 has four layouts: horizontal (Qhboxlayout), Vertical (qvboxlayout), Grid (Qgridlayout), form (qformlayout)
A single layout in a form should be easy, but if you have a more complex layout, it usually involves nesting of layouts, which is a headache.

The four knowledge points in this article are:
1. Layout cannot be nested directly (if I am wrong, please correct me!) )
2. The layout of the inner layer must first " attach " to an empty qwidget
3. Add the empty part that " hosts " The inner layout to the outer layout
4. Finally, don't forget to "attach" the global layout to the form's

0. Look First

The layout is analyzed below

1. Global Layout Analysis

The global layout uses a horizontal layout

Wlayout = Qtwidgets.qhboxlayout ()

2. Local layout analysis

Four local layouts use horizontal layout, vertical layout, grid layout, form layout, respectively

Hlayout = Qtwidgets.qhboxlayout () vlayout = Qtwidgets.qvboxlayout () glayout = Qtwidgets.qgridlayout () Flayout = Qtwidgets.qformlayout ()

Here, I actually started out with a natural, but wrong idea: To add four local layouts directly to the global layout:

Wlayout.addwidget (hlayout) wlayout.addwidget (vlayout) wlayout.addwidget (glayout) wlayout.addwidget (flayout)

Error message Tip: The AddWidget () parameter must be of type Qwidget!

This gave me a revelation:

First prepare four QWIDGET:HWG, VWG, GWG, FWG

HWG = Qtwidgets.qwidget () VWG = Qtwidgets.qwidget () GWG = Qtwidgets.qwidget () FWG = Qtwidgets.qwidget ()

Then use these four qwidget to set the first four local layouts separately

Hwg.setlayout (hlayout) vwg.setlayout (vlayout) gwg.setlayout (glayout) fwg.setlayout (flayout)

and add these four qwidget parts to the global variable.

Wlayout.addwidget (HWG) wlayout.addwidget (VWG) wlayout.addwidget (GWG) wlayout.addwidget (FWG)

The final step is to apply the global layout to the form's

Self.setlayout (Wlayout)

3. Complete code
 fromPyQt5ImportqtwidgetsclassMywindow (qtwidgets.qwidget):def __init__(self): Super ().__init__() Self.setwindowtitle ('PyQt5 Layout Example')                #Start:Wlayout = Qtwidgets.qhboxlayout ()#Global Layout (1): HorizontalHlayout= Qtwidgets.qhboxlayout ()#Local Layout (4): horizontal, vertical, grid, formVlayout =qtwidgets.qvboxlayout () glayout=qtwidgets.qgridlayout () flayout=qtwidgets.qformlayout () hlayout.addwidget (Qtwidgets.qpushbutton (str (1)))#Local layout Add part (for example: button)Hlayout.addwidget (Qtwidgets.qpushbutton (str (2)) Vlayout.addwidget (Qtwidgets.qpushbutton (str ) (3)) Vlayout.addwidget (Qtwidgets.qpushbutton (str ) (4)) Glayout.addwidget (Qtwidgets.qpushbutton (str ) (5) , 0,0) glayout.addwidget (Qtwidgets.qpushbutton (str (6)), 0,1) Glayout.addwidget (Qtwidgets.qpushbutton (str (7)), 1, 0) glayout.addwidget (Qtwidgets.qpushbutton (str (8)),) Flayout.addwidget (Qtwidgets.qpushbutton (str (9)) Flayout.addwidget (Qtwidgets.qpushbutton (str ) (10)) Flayout.addwidget (Qtwidgets.qpushbutton (str ) (11)) Flayout.addwidget (Qtwidgets.qpushbutton (str ) (12)) HWG= Qtwidgets.qwidget ()#prepare four partsVWG =qtwidgets.qwidget () GWG=qtwidgets.qwidget () FWG=Qtwidgets.qwidget () hwg.setlayout (hlayout)#Four parts set up a local layoutvwg.setlayout (vlayout) gwg.setlayout (glayout) fwg.setlayout (flayout) WLAYOUT.ADDW Idget (HWG)#four parts added to the global layoutwlayout.addwidget (VWG) wlayout.addwidget (GWG) wlayout.addwidget (FWG) Self.setlayo UT (wlayout)#form the master set global layout          if __name__=="__main__":        Importsys app=Qtwidgets.qapplication (SYS.ARGV) win=Mywindow () win.show () Sys.exit (App.exec_ ())

PyQt5 Notes (01): 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.