PDA

View Full Version : Trying to refresh a QGridLayout



zlacelle
8th July 2008, 21:25
I've used Qt Designer to create a QWidget with a grid layout on it, containing checkboxes which are dynamically created in my code and correspond to a number in a file on my computer. When a file is changed on the machine, I need the layout to refresh with a number of check boxes equal to a new specified number in the file. I'm trying to use the following code:


//Refresh the layout
ui.TPCheckBoxLayout_G->invalidate();
ui.TPCheckBoxLayout_G->activate();
ui.TPCheckBoxLayout_G->update();
QWidget::repaint();

I call this code right after I delete all of the items from the layout, using the following code:


//Refresh checkboxes and buttons
int rowCount = ui.TPCheckBoxLayout_G->rowCount();
int columnCount = ui.TPCheckBoxLayout_G->columnCount();
for(int i=0; i<rowCount; i++)
{
for(int j=0; j<columnCount; j++)
{
//Clear the TP layout
ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
}
}
}

However, after this code finished, the new boxes are present, but they appear over the old boxes, which don't exist in my code! My new stuff appears, but the old stuff doesn't go away. Any idea what I'm doing wrong?

wysota
8th July 2008, 21:30
When you modify a layout by adding or removing a widget from it, it gets updated by itself.

Your problem is that removing widgetss from the layout doesn't cause them to be deleted. You are deleting layout items, not widgets contained in them.

zlacelle
8th July 2008, 21:37
Then what steps should I take to properly delete the old items? Since I create them dynamically, they all have the same name and I don't have access to it. Code to add check boxes:



msCount = 0;
[loop]
QCheckBox* chb = new QCheckBox();
chb->setText(tempName);
chb->setCheckState(Qt::Unchecked);
connect(chb, SIGNAL(stateChanged(int)), this, SLOT(on_checkbox_change_MS(int)));
ui.MSCheckBoxLayout_G->addWidget(chb,0,msCount);
msCount++;
[end of loop]

zlacelle
8th July 2008, 21:49
I figured it out...here's help to anybody who comes across this in the future. Pretty obvious, but it took me a while to figure it out... :)


//Clear the TP layout
QWidget* widget = ui.TPCheckBoxLayout_G->itemAtPosition(i,j)->widget(); ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
delete widget;

wysota
8th July 2008, 21:52
It would be simpler to store a list of widgets:

QList<QWidget *> widgets;
QWidget *w = new ....;
widgets <<w;
Then you could just do:

qDeleteAll(widgets);
widgets.clear();

Alternatively if all your widgets are checkboxes (or some other particular widget classes), you can create the list on demand:

QList<QCheckBox*> list = findChildren<QCheckBox*>();
qDeleteAll(list);

Then you can insert new widgets into the layout...

chrisnew
5th April 2009, 21:21
I figured it out...here's help to anybody who comes across this in the future. Pretty obvious, but it took me a while to figure it out... :)


//Clear the TP layout
QWidget* widget = ui.TPCheckBoxLayout_G->itemAtPosition(i,j)->widget(); ui.TPCheckBoxLayout_G->removeItem(ui.TPCheckBoxLayout_G->itemAtPosition(i,j));
delete ui.TPCheckBoxLayout_G->itemAtPosition(i,j);
delete widget;

Using Qt 4.5 on Linux I found that the index count needs resetting to 0 each time so I've just used this:



while(layout->count() > 0) {
QWidget* widget = layout->itemAt(0)->widget();
layout->removeWidget(widget);
delete widget;
}


...or alternatively remove the items in reverse from the end of the layout.

chris