qt - Allignment of a QLabel and a QCheckBox in a BoxLayout gives unexpected result -


when add qlabel , qcheckboxs either qvboxlayout or qhboxlayout expect them evenly distributed checkboxes allign tight @ bottom (in above example) , label centered on resulting free space in widget. how can change behaviour distribute 3 widgets evenly?

many thanks.

this example code:

widget::widget(qwidget *parent) :     qwidget(parent),     ui(new ui::widget) {     qlabel* l = new qlabel("hi");     qcheckbox* c = new qcheckbox("label");     qcheckbox* c2 = new qcheckbox("label");     l->settext("hi");     qvboxlayout* v = new qvboxlayout;      v->addwidget(l);     v->addwidget(c);     v->addwidget(c2);     setlayout(v);     ui->setupui(this); } 

and result:

result of code example

take @ qsizepolicy. need setsizepolicy qlabel , qcheckboxes qsizepolicy::preferred, docs:

the default policy preferred/preferred, means widget can freely resized, prefers size sizehint() returns. button-like widgets set size policy specify may stretch horizontally, fixed vertically. same applies lineedit controls (such qlineedit, qspinbox or editable qcombobox) , other horizontally orientated widgets (such qprogressbar).

currently, qlabel has preferred height, while both of qcheckboxes has fixed height. means qlabel expanded automatically take additional vertical space (that can't taken qcheckboxes.

so, in order set widgets preferred height, need add following constructor:

l->setsizepolicy(qsizepolicy::preferred, qsizepolicy::preferred); c->setsizepolicy(qsizepolicy::preferred, qsizepolicy::preferred); c2->setsizepolicy(qsizepolicy::preferred, qsizepolicy::preferred); 

another option, add spacers around each 1 of widgets, this:

v->addstretch(); v->addwidget(l); v->addstretch(); v->addwidget(c); v->addstretch(); v->addwidget(c2); v->addstretch(); setlayout(v); 

this way qspaceritems take additional space.


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -