c++ - Qt: Resize QScrollArea to show 4 widgets at most -


i dynamically adding , removing widgets in qscrollarea, , show @ 4 widgets @ same time before scroll bar appears. basically, if have 0-3 widgets , add one, scroll area resized fit new height, after that, height stays @ 4 widgets size , have scroll see 5th, 6th, ...

currently, call following method when widget inserted/removed.

void widgetlist::resizescrollarea() {     // height of first 4 widgets     int widgetsheight = 0;     (int = 0; < _widgets.size() && < 4; ++i)     {         // height of widget         widgetsheight += _widgets.at(i)->sizehint().height();     }     // leeway make sure have gap between widgets     _ui->scrollarea->setfixedheight(widgetsheight + 5); } 

the problem sizehint() isn't correct height (sometimes big), neither size() (often small). scroll area bit big content, works.
not widgets have same height, , sizehint seems correct one, , size.
understand sizehint size widget have, not 1 layout gives it, don't why size incorrect.

any idea on how should appreciated.

instead of setting fixed height, should override sizehint() return size widget (so layout give extra, or bit less, if needs to). want call invalidate() whenever of first 4 child widgets changed, tell containing layout previous cached values should recomputed.

and if widgets in qvboxlayout (i'm guessing, seems reasonable assume), should retrieving spacing , top margin in computation.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -