qt - Allignment of a QLabel and a QCheckBox in a BoxLayout gives unexpected result -
when add qlabel
, qcheckbox
s 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:
take @ qsizepolicy
. need setsizepolicy
qlabel
, qcheckbox
es qsizepolicy::preferred
, docs:
the default policy
preferred
/preferred
, means widget can freely resized, prefers sizesizehint()
returns. button-like widgets set size policy specify may stretch horizontally, fixed vertically. same applies lineedit controls (suchqlineedit
,qspinbox
or editableqcombobox
) , other horizontally orientated widgets (suchqprogressbar
).
currently, qlabel
has preferred height, while both of qcheckbox
es has fixed height. means qlabel
expanded automatically take additional vertical space (that can't taken qcheckbox
es.
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 qspaceritem
s take additional space.
Comments
Post a Comment