PDA

View Full Version : remove item for QGirdLayout doesn't work.



klnusbaum
22nd May 2008, 19:50
I'm trying to dynamically create QComboBoxes and QLabels during run time based on the users input. Example: if the users enters 3, my program displays 3 comobboxes and 3 labels. If they then type in 2, my program will then display only 2 comboboxes and 2 labels. When ever the user changes the number of comboboxes and labels this function is called (numOfSurfaces is the user input that the number of comboboxes and labels depend on):


for(int row=0; row<surf_typeGrid->rowCount(); row++)
{
surf_typeGrid->removeItem(surf_typeGrid->itemAtPosition(row,0));
surf_typeGrid->removeItem(surf_typeGrid->itemAtPosition(row,1));
}

int numOfSurfaces = (nwall_typeLine->displayText()).toInt();
for(int i=1; i<=numOfSurfaces; i++)
{
char surfLabel [15];
sprintf(surfLabel, "Surface %d", i);
surf_typeGrid->addWidget(new QLabel(surfLabel), i-1, 0);
surf_typeGrid->addWidget(new QComboBox(), i-1,1);
}

The problem is that when the user changes the number of comboboxes and labels, instead of deleting the old comboboxes and lables and displaying just the new ones, the program still displays the old comboxes and labels and just puts the new ones on top of them. How do I make sure the old comboboxes and labels get removed?

jpn
22nd May 2008, 21:16
From QLayout::removeItem() docs:


Removes the layout item item from the layout. It is the caller's responsibility to delete the item.

In other words, QLayout::removeItem() removes the item from the layout in the sense that the geometry of the item is no more managed by the layout but that's all. The item (nor the widget) is not deleted.

klnusbaum
22nd May 2008, 22:43
You're exactly right. I wasn't actually deleting them. When I realized this, I figured it out. Being new to c++, I'm evidently struggling with pointers. Here's my solution in case anyone else needs it.


void SurfCntrlParams::confSurf_typeInputLayout(const QString & text)
{
std::vector<QComboBox*>::iterator it = surf_typeBoxVector->begin();
std::vector<QLabel*>::iterator it2 = surf_typeLabelVector->begin();

for(;it<(surf_typeBoxVector->end()); it++, it2++)
{
delete *it2;
delete *it;
}

surf_typeBoxVector->clear();
surf_typeLabelVector->clear();

int numOfSurfaces = text.toInt();
for(int i=1; i<=numOfSurfaces; i++)
{
char surfLabel [15];
sprintf(surfLabel, "Surface %d", i);
surf_typeBoxVector->push_back(new QComboBox());
(surf_typeBoxVector->back())->addItems(*surf_typeList);
surf_typeLabelVector->push_back(new QLabel(surfLabel));
}

it = surf_typeBoxVector->begin();
it2 = surf_typeLabelVector->begin();
for(int row2=0;it<(surf_typeBoxVector->end()); row2++,it++, it2++)
{
surf_typeGrid->addWidget(*it2,row2,0);
surf_typeGrid->addWidget(*it,row2,1);
}
}

jpn
23rd May 2008, 07:37
Have you considered using model-view (http://doc.trolltech.com/4.4/model-view-programming.html) approach..?

klnusbaum
23rd May 2008, 23:04
Wow, that model view thing looks like it might just be exactly what the doctor ordered. Wow. Thanks a bunch jpn. I'll have to look into that more.