Briefly
Qflowlayout, as the name implies-flow layout, implements the layout that handles different window sizes. Changes the placement of controls based on the width of the application window.
Specific implementation requirements are no longer mentioned, please refer to the first two sections.
Realize
Qflowlayout is mainly implemented using Qlayout and Qwidgetitem, while Windows uses Qwidget and Qpushbutton.
Effect
Source
QFlowLayout.h
#ifndef Qflowlayout_h#define qflowlayout_h#include <QLayout>#include <QRect>#include <QStyle>Class Qflowlayout: Publicqlayout{ Public:Explicit Qflowlayout(Qwidget *parent,intMargin =-1,inthspacing =-1,intvspacing =-1);ExplicitQflowlayout (intMargin =-1,inthspacing =-1,intvspacing =-1); ~qflowlayout ();voidAddItem (Qlayoutitem *item) q_decl_override;intHorizontalspacing ()Const;intVerticalspacing ()Const; Qt::orientations expandingdirections ()ConstQ_decl_override;BOOLHasheightforwidth ()ConstQ_decl_override;intHeightforwidth (int)ConstQ_decl_override;intCount ()ConstQ_decl_override; Qlayoutitem *itemat (intIndexConstQ_decl_override; Qsize MinimumSize ()ConstQ_decl_override;voidSetgeometry (ConstQrect &rect) Q_decl_override; Qsize Sizehint ()ConstQ_decl_override; Qlayoutitem *takeat (intindex) Q_decl_override;Private:int Dolayout(ConstQrect &rect,BOOLTESTONLY)Const;intSmartspacing (qstyle::P ixelmetric pm)Const;Private: Qlist<qlayoutitem *> itemList;intM_hspace;intM_vspace;};#endif //Qflowlayout_h
QFlowLayout.cpp
#include <QWidget>#include "QFlowLayout.h"Qflowlayout::qflowlayout (Qwidget *parent,intMarginintHspacing,intvspacing): Qlayout (parent), M_hspace (hspacing), M_vspace (vspacing) {setcontentsmargins (margin, margin, MA Rgin, margin);} Qflowlayout::qflowlayout (intMarginintHspacing,intvspacing): M_hspace (hspacing), M_vspace (vspacing) {setcontentsmargins (margin, margin, margin, margin);} Qflowlayout::~qflowlayout () {Qlayoutitem *item; while(item = Takeat (0)))DeleteItem;}voidQflowlayout::additem (Qlayoutitem *item) {itemlist.append (item);}intQflowlayout::horizontalspacing ()Const{if(M_hspace >=0) {returnM_hspace; }Else{returnSmartspacing (qstyle::P m_layouthorizontalspacing); }}intQflowlayout::verticalspacing ()Const{if(M_vspace >=0) {returnM_vspace; }Else{returnSmartspacing (qstyle::P m_layoutverticalspacing); }}intQflowlayout::count ()Const{returnItemlist.size ();} Qlayoutitem *qflowlayout::itemat (intIndexConst{returnItemlist.value (index);} Qlayoutitem *qflowlayout::takeat (intIndex) {if(Index >=0&& Index < itemlist.size ())returnItemlist.takeat (index);Else return 0;} Qt::orientations qflowlayout::expandingdirections ()Const{return 0;}BOOLQflowlayout::hasheightforwidth ()Const{return true;}intQflowlayout::heightforwidth (intWidthConst{intHeight = dolayout (qrect (0,0, Width,0),true);returnHeight;}voidQflowlayout::setgeometry (ConstQrect &rect) {qlayout::setgeometry (rect); Dolayout (Rect,false);} Qsize Qflowlayout::sizehint ()Const{returnMinimumSize ();} Qsize qflowlayout::minimumsize ()Const{qsize size; Qlayoutitem *item; foreach (item, itemList) size = Size.expandedto (Item->minimumsize ()); Size + = Qsize (2*margin (),2*margin ());returnSize;}intQflowlayout::d olayout (ConstQrect &rect,BOOLTESTONLY)Const{intLeft, top, right, bottom; Getcontentsmargins (&left, &top, &right, &bottom); Qrect effectiverect = rect.adjusted (+left, +top,-right,-bottom);intx = Effectiverect.x ();inty = Effectiverect.y ();intLineheight =0; Qlayoutitem *item; foreach (item, itemList) {Qwidget *wid = Item->widget ();intSpaceX = Horizontalspacing ();if(SpaceX = =-1) SpaceX = Wid->style ()->layoutspacing (qsizepolicy::P Ushbutton, Qsizepolicy::P ush Button, Qt::horizontal);intSpaceY = Verticalspacing ();if(SpaceY = =-1) SpaceY = Wid->style ()->layoutspacing (qsizepolicy::P Ushbutton, Qsizepolicy::P ush Button, qt::vertical);intNEXTX = x + item->sizehint (). Width () + SpaceX;if(Nextx-spacex > Effectiverect.right () && lineheight >0) {x = Effectiverect.x (); y = y + lineheight + spaceY; NEXTX = x + item->sizehint (). Width () + SpaceX; Lineheight =0; }if(!testonly) Item->setgeometry (Qrect (Qpoint (x, y), Item->sizehint ())); x = NEXTX; Lineheight = QMax (Lineheight, Item->sizehint (). height ()); }returnY + lineheight-rect.y () + bottom;}intQflowlayout::smartspacing (qstyle::P ixelmetric pm)Const{Qobject *parent = This->parent ();if(!parent) {return-1; }Else if(Parent->iswidgettype ()) {Qwidget *PW =static_cast<qwidget *> (parent);returnPw->style ()->pixelmetric (PM,0, PW); }Else{return static_cast<qlayout *> (parent)->spacing (); }}
Qt's Custom layout manager (Qflowlayout)