Briefly
Manual layout The other way is to write your own layout manager by inheriting the Qlayout class.
Let's give an example-qcardlayout in detail below. It is inspired by the Java layout manager with the same name. Also known as the card layout, each item is offset by qlayout::spacing ().
Defined
To write your own layout, you must define the following:
A data structure that stores layout-processing items, each of which is a qlayoutitem, and this example uses Qlist.
AddItem () How to add a project layout.
Setgeometry () How to control the layout.
The preferred size of the sizehint () layout.
Itemat () How to traverse the layout.
Takeat () How to delete items in a layout.
In most cases, the minimumsize () will also be implemented.
Achieve results
Source
QCardLayoud.h
#ifndef Qcardlayout_h#define QCARDLAYOUT_H#include <QWidget>#include <QLayout>#include <QLayoutItem> class qcardlayout : public qlayout{ Public: Qcardlayout (Qwidget *parent =0); ~qcardlayout ();voidAddItem (Qlayoutitem *item); Qsize Sizehint ()Const; Qsize MinimumSize ()Const;intCount ()Const; Qlayoutitem *itemat (int)Const; Qlayoutitem *takeat (int);voidSetgeometry (ConstQrect &rect);Private: Qlist<qlayoutitem*> list;};#endif//qcardlayout_h
QCardLayoud.cpp
#include "QCardLayout.h"Qcardlayout::qcardlayout (Qwidget *parent): qlayout (parent) {}//Because Qlayoutitem does not inherit from Qobject, it must be deleted manually. In the destructor, use Takeat () to delete each item in the list and then delete it. Qcardlayout::~qcardlayout () {Qlayoutitem *item; while(item = Takeat (0)))DeleteItem;}//Gets the number of items in the listintQcardlayout::count ()Const{return List. Size ();}//Get index of the item corresponding to IDXQlayoutitem *qcardlayout::itemat (intIdxConst{return List. value (idx);}//Remove the item corresponding to the index IDX and returnQlayoutitem *qcardlayout::takeat (intIDX) {returnIDX >=0&& IDX <List. Size ()?List. Takeat (IDX):0;}//Add ItemvoidQcardlayout::additem (Qlayoutitem *item) {List. append (item);}//The layout is actually controlled, and the rectangle provided as a parameter does not include margin (). Related, use spacing () as the distance between items. voidQcardlayout::setgeometry (ConstQrect &r) {qlayout::setgeometry (R);if(List. Size () = =0)return;intW = r.width ()-(List. Count ()-1) * spacing ();inth = r.height ()-(List. Count ()-1) * spacing ();inti =0; while(I <List. Size ()) {Qlayoutitem *o =List. at (i); Qrect Geom (r.x () + i * spacing (), r.y () + i * spacing (), W, h); O->setgeometry (GEOM); ++i; }}//Sizehint () and minimumsize () are usually very similar. The dimensions returned by these two functions should include spacing (), but not margin (). Qsize Qcardlayout::sizehint ()Const{qsize S (0,0);intn =List. Count ();if(N >0) s = qsize ( -, -);inti =0; while(I < N) {Qlayoutitem *o =List. at (i); s = s.expandedto (O->sizehint ()); ++i; }returnS + n*qsize (spacing (), spacing ());} Qsize qcardlayout::minimumsize ()Const{qsize S (0,0);intn =List. Count ();inti =0; while(I < N) {Qlayoutitem *o =List. at (i); s = s.expandedto (O->minimumsize ()); ++i; }returnS + n*qsize (spacing (), spacing ());}
Qt's Custom layout manager (Qcardlayout)