Results 1 to 2 of 2

Thread: Adding widgets dynamically

  1. #1
    Join Date
    Jun 2009

    Default Adding widgets dynamically

    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 ) 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 :

    Qt Code:
    1. flowLayout->addWidget(t);
    3. flowLayout->invalidate();
    4. flowLayout->activate();
    5. flowLayout->update();
    6. t->show();
    7. show();
    8. updateGeometry();
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jul 2008
    Qt products

    Default Re: Adding widgets dynamically

    you need to add a groupbox too

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

Similar Threads

  1. Dynamic widgets adding and connecting signals& slots
    By jjbabu in forum Qt Programming
    Replies: 2
    Last Post: 22nd May 2009, 13:36
  2. Replies: 4
    Last Post: 2nd December 2008, 05:19
  3. adding widgets to tabWidgets pages
    By quickNitin in forum Newbie
    Replies: 1
    Last Post: 23rd November 2006, 10:59
  4. Dynamically adding tabs
    By larry104 in forum Qt Programming
    Replies: 7
    Last Post: 26th July 2006, 21:27
  5. Adding/removing widgets dynamically...
    By ReilenBlaeyze in forum Newbie
    Replies: 2
    Last Post: 16th February 2006, 11:55


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.