PDA

View Full Version : Adding widgets dynamically



miw
27th June 2009, 23:20
Hello
I have a problem with dynamically adding widgets to a layout, and any help would be really appreciated.

Here's what I do : I add widgets (QPushButton's) dynamically to a layout, that is I don't create them in the constructor but in a method (a SLOT). The problem is my layout (being here a FlowLayout that I got from here http://doc.trolltech.com/4.5/layouts-flowlayout.html ) doesn't resize when I add widgets, and stays at its minimumSize. Which means that if I add too many widgets you can't see them because the parent widget isn't big enough. Basically what I would want the layout to do is resize to take as much space as it wants (since it's in a QScrollArea anyway) just as it would if I had created the widgets in the constructor.
I searched the web and tried everything I found but to no avail. Here's everything I tried :


flowLayout->addWidget(t);

flowLayout->invalidate();
flowLayout->activate();
flowLayout->update();
t->show();
show();
updateGeometry();

To see if the problem came from the custom layout, I tried changing it to a QVBoxLayout and got a slightly better result since you can see all the new buttons, but what the layout does is not resize itself, but shrink the buttons and reduce the spacing so that they will all fit in the same space, which means that if you add too many buttons they all end up crammed together and you can't see anything.

The only approach that did work was manually changing the minimumSize after each new button was added to set it to a value that I compute depending on the number of buttons. But not only is it un ugly approach, it's also quite buggy and gives me a different result on a Mac.

It seams like it would be a pretty common problem but I couldn't find anything helpful on the web, so thanks in advance if you have the solution.

fmariusd
23rd July 2009, 11:31
you need to add a groupbox too

scrollArea-> groupbox->layout->items....