PDA

View Full Version : How to remove widget added in runtime?



jiveaxe
1st November 2007, 09:26
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):


MainDialog::MainDialog(QWidget *parent)
:QDialog(parent)
{
...
groupBox = new QGroupBox("Clauses");
...
groupBoxLayout = new QVBoxLayout(groupBox);
...
removeConditionButton->setEnabled(false);
...
connect(addConditionButton, SIGNAL(clicked()), this, SLOT(addCondition()));
connect(removeConditionButton, SIGNAL(clicked()), this, SLOT(removeCondition()));
}

void MainDialog::addCondition()
{
QComboBox *comboBox = new QComboBox;
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

marcel
1st November 2007, 09:54
You also need to remove the widget from the layout:


QWidget *w = list.takeLast();
w->hide();
groupBoxLayout->removeWidget(w);
delete w;

momesana
1st November 2007, 10:01
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.

jiveaxe
1st November 2007, 10:14
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:


groupBoxLayout->setDirection(QBoxLayout::BottomToTop);

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

Bye

marcel
1st November 2007, 10:35
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.

jiveaxe
1st November 2007, 11:28
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

jpn
1st November 2007, 13:55
I have found a solution; using QSpacerItem and QLayout::addItem/QLayout::removeItem
Notice QBoxLayout::addSpacing() and QBoxLayout::insertSpacing(). Easier to read and write. ;)

jiveaxe
2nd November 2007, 11:13
Ok jpn, I'll give them a try.

Thank