Results 1 to 8 of 8

Thread: How to remove widget added in runtime?

  1. #1
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to remove widget added in runtime?

    Hi,
    i'm working on a database application; i would like to add the possibility for the user to perform custom query with AND/OR clauses. So i thought to add to my program 2 button, one for adding and one for removing clauses; each clause is a custom widget; the list of clause/widget is stored in a QList. The remove function should delete the last added clause from QList and from gui. My problem is I can't remove widgets from the gui after adding them. Here is some code of a sample application i made for the thread (no database handling):

    Qt Code:
    1. MainDialog::MainDialog(QWidget *parent)
    2. :QDialog(parent)
    3. {
    4. ...
    5. groupBox = new QGroupBox("Clauses");
    6. ...
    7. groupBoxLayout = new QVBoxLayout(groupBox);
    8. ...
    9. removeConditionButton->setEnabled(false);
    10. ...
    11. connect(addConditionButton, SIGNAL(clicked()), this, SLOT(addCondition()));
    12. connect(removeConditionButton, SIGNAL(clicked()), this, SLOT(removeCondition()));
    13. }
    14.  
    15. void MainDialog::addCondition()
    16. {
    17. QComboBox *comboBox = new QComboBox;
    18. groupBoxLayout->addWidget(comboBox);
    19. list.append(comboBox);
    20. removeConditionButton->setEnabled(true);
    21. }
    22.  
    23. void MainDialog::removeCondition()
    24. {
    25. list.removeLast();
    26. if(list.size() == 0)
    27. removeConditionButton->setEnabled(false);
    28. }
    To copy to clipboard, switch view to plain text mode 

    What have I to add to removeCondition()? Or is there a better solution?

    Thanks
    Last edited by jiveaxe; 1st November 2007 at 10:29. Reason: spelling error
    Giuseppe CalÃ

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How remove widger added in runtime?

    You also need to remove the widget from the layout:
    Qt Code:
    1. QWidget *w = list.takeLast();
    2. w->hide();
    3. groupBoxLayout->removeWidget(w);
    4. delete w;
    To copy to clipboard, switch view to plain text mode 

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

    jiveaxe (1st November 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    258
    Thanks
    22
    Thanked 19 Times in 16 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How remove widger added in runtime?

    I'm not sure I understand what you want do but you can add and remove widgets from a layout by using Layout::removeWidget(). In your case it may be better to hide/show the widget in contrast to removing it from the layout and deleting it afterwards.
    See QWidget::setVisible(bool b), QWidget::show(), QWidget::hide() for more details.

  5. #4
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How remove widger added in runtime?

    Thank you marcel,
    your help is always on time.

    One more question: when adding widgets they are placed distant each other occupying the height of the container; how place them grouped from the begin with free space at bottom; i got a layout near it with:

    Qt Code:
    1. groupBoxLayout->setDirection(QBoxLayout::BottomToTop);
    To copy to clipboard, switch view to plain text mode 

    but the widgets are placed (obviously) inverted; the last added is at top of the container.

    Bye
    Giuseppe CalÃ

  6. #5
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How remove widger added in runtime?

    You can try adding a spacer in the vertical layout after you add all the widget. I should occupy all the free space and push the widgets up.

  7. #6
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to remove widget added in runtime?

    I have found a solution; using QSpacerItem and QLayout::addItem/QLayout::removeItem I remove the spacer before adding a new widget and add it after; the widgets are now grouped at top of container.

    Bye
    Giuseppe CalÃ

  8. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,518 Times in 1,388 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How to remove widget added in runtime?

    Quote Originally Posted by jiveaxe View Post
    I have found a solution; using QSpacerItem and QLayout::addItem/QLayout::removeItem
    Notice QBoxLayout::addSpacing() and QBoxLayout::insertSpacing(). Easier to read and write.
    J-P Nurmi

  9. #8
    Join Date
    Aug 2007
    Posts
    244
    Thanks
    42
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to remove widget added in runtime?

    Ok jpn, I'll give them a try.

    Thank
    Giuseppe CalÃ

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.