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):
Code:
MainDialog
::MainDialog(QWidget *parent
){
...
...
...
removeConditionButton->setEnabled(false);
...
connect(addConditionButton, SIGNAL(clicked()), this, SLOT(addCondition()));
connect(removeConditionButton, SIGNAL(clicked()), this, SLOT(removeCondition()));
}
void MainDialog::addCondition()
{
groupBoxLayout->addWidget(comboBox);
list.append(comboBox);
removeConditionButton->setEnabled(true);
}
void MainDialog::removeCondition()
{
list.removeLast();
if(list.size() == 0)
removeConditionButton->setEnabled(false);
}
What have I to add to removeCondition()? Or is there a better solution?
Thanks
Re: How remove widger added in runtime?
You also need to remove the widget from the layout:
Code:
w->hide();
groupBoxLayout->removeWidget(w);
delete w;
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.
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:
Code:
groupBoxLayout
->setDirection
(QBoxLayout::BottomToTop);
but the widgets are placed (obviously) inverted; the last added is at top of the container.
Bye
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.
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
Re: How to remove widget added in runtime?
Quote:
Originally Posted by
jiveaxe
I have found a solution; using QSpacerItem and QLayout::addItem/QLayout::removeItem
Notice QBoxLayout::addSpacing() and QBoxLayout::insertSpacing(). Easier to read and write. ;)
Re: How to remove widget added in runtime?
Ok jpn, I'll give them a try.
Thank