Results 1 to 3 of 3

Thread: [solved]Dynamically removing QPushButtons from Flowlayout

  1. #1
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question [solved]Dynamically removing QPushButtons from Flowlayout

    Hi all,

    I read a few tutorials and started to experiment with qt but I ran into a problem I just don't understand.
    So, I'm trying to add and remove QPushButtons to/from a flowlayout (the one from the examples here) which works quite well except for the last item.
    Here is my code:

    Qt Code:
    1. MyWidget::MyWidget(QWidget *parent) :
    2. QWidget(parent)
    3. {
    4.  
    5.  
    6. deletebutton = new QPushButton("delete",this);
    7. addbutton = new QPushButton("add",this);
    8.  
    9. hbox = new QHBoxLayout();
    10. hbox->addWidget(deletebutton);
    11. hbox->addWidget(addbutton);
    12.  
    13. QWidget* widget = new QWidget(this);
    14. flowlayout = new FlowLayout(widget);
    15. widget->setLayout(flowlayout);
    16. QScrollArea *scrollArea = new QScrollArea();
    17. scrollArea->setWidgetResizable(true);
    18. scrollArea->setWidget(widget);
    19.  
    20. vbox = new QVBoxLayout(this);
    21. vbox->addLayout(hbox);
    22. vbox->addWidget(scrollArea);
    23.  
    24. setLayout(vbox);
    25.  
    26. connect(deletebutton, SIGNAL(clicked()), this, SLOT(deleteButton()));
    27. connect(addbutton, SIGNAL(clicked()), this, SLOT(addButton()));
    28.  
    29. }
    30.  
    31. void MyWidget::deleteButton() {
    32.  
    33.  
    34. flowlayout->takeAt(0);
    35. flowlayout->update();
    36.  
    37. qDebug() <<"deleted: "<<flowlayout->count();
    38. }
    39.  
    40. void MyWidget::addButton() {
    41.  
    42. QPushButton* newButton = new QPushButton("button");
    43. flowlayout->addWidget(newButton);
    44. flowlayout->update();
    45. connect(newButton, SIGNAL(clicked()), this, SLOT(buttonpressed()));
    46.  
    47. qDebug() <<"added: "<<flowlayout->count();
    48. }
    49.  
    50. void MyWidget::buttonpressed() {
    51.  
    52.  
    53. qDebug() <<"button pressed";
    54.  
    55. }
    To copy to clipboard, switch view to plain text mode 

    When I want to delete the last button, I get the correct qDebug() output (0), but the QPushButton does not disappear and when I click on it, it still produces the "button pressed" output. Everything else works just fine. I can add buttons and every button disappears when I click on delete.

    What am I doing wrong?

    Thanks!
    Last edited by neff; 30th January 2013 at 16:47.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically removing QPushButtons from Flowlayout

    How about this :
    Qt Code:
    1. void MyWidget::deleteButton()
    2. {
    3. QLayoutItem *item = flowlayout->takeAt(0);
    4. if( item )
    5. {
    6. delete item;
    7. }
    8.  
    9. qDebug() <<"deleted: "<<flowlayout->count();
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lesiok for this useful post:

    neff (30th January 2013)

  4. #3
    Join Date
    Jan 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Dynamically removing QPushButtons from Flowlayout

    Thank you for your reply.
    Unfortunately the proposed correction doesn't solve the problem.
    If I omit "flowlayout->update()" nothing in my program changes after I press the delete button (all Buttons remain in the flowlayout and all buttons still work). If I add "flowlayout->update()" to your correction, the program behaviour is the same as explained in the top post.

    I tried a bit more and now I have the behaviour I aimed for with:
    Qt Code:
    1. void MyWidget::deleteButton() {
    2.  
    3. QLayoutItem *item = flowlayout->takeAt(0);
    4.  
    5. if( item )
    6. {
    7. item->widget()->hide();
    8. delete item->widget();
    9. delete item;
    10. }
    11.  
    12. qDebug() <<"deleted: "<<flowlayout->count();
    13. }
    To copy to clipboard, switch view to plain text mode 

    delete item->widget(); makes the button disappear from the layout.
    item->widget()->hide(); makes sure that the buttons that are left get rearranged.
    Last edited by neff; 30th January 2013 at 16:44.

Similar Threads

  1. Replies: 6
    Last Post: 13th August 2011, 19:31
  2. Replies: 0
    Last Post: 27th October 2010, 16:57
  3. Where is my QPushButtons
    By HelloDan in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2009, 08:15
  4. Mouse Over Event on QPushButtons
    By merry in forum Qt Programming
    Replies: 2
    Last Post: 6th August 2007, 14:23
  5. Adding/removing widgets dynamically...
    By ReilenBlaeyze in forum Newbie
    Replies: 2
    Last Post: 16th February 2006, 11:55

Tags for this Thread

Bookmarks

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.