PDA

View Full Version : QListWidget->clear()



Charvi
13th August 2012, 09:07
Hi,

I want to clear my QListWidget (empty the list) before refilling and repopulating it. But when I use QlistWidget->clear() for this purpose the program terminates with a backtrace:

inside updatelist
*** glibc detected *** /home/charvi123/QtSDK/Projects/scanIt-build-desktop/scanIt: free(): invalid pointer: 0x0903f35c ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6(+0x6b161)[0x617161]
/lib/tls/i686/cmov/libc.so.6(+0x6c9b8)[0x6189b8]
/lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0x61ba9d]
.
.
.

I am completely lost. How do I clear my list so that it shows the refreshed contents.

Thanks in advance,
-Charvi.

yeye_olive
13th August 2012, 09:14
Could you post the whole code of your program, please? The crash is most likely caused by some bug in another part of the program which merely manifests itself when you call QListWidget::clear().

Charvi
13th August 2012, 09:21
This happens every second time I try to free the list. The first time the list is populated nicely. What I am doing is just adding the items to the list and repopulating the list by first clearing it and then loading items from a QListWidgetItem array.

And when I don't clear the list the program just runs fine.

Is there any other method through which I could achieve the emptying of the list widget ?

yeye_olive
13th August 2012, 09:44
Like I said, there is not much we can do (besides guessing) until we see some code. Here is a guess; perhaps your program double-deletes the items:
- once when your QListWidgetItem array goes out of scope,
- once when you call clear(). Remember that according to the docs, QListWidget::insertItem(QListWidgetItem *item) takes ownership of item.

Charvi
13th August 2012, 10:06
- My QListWidgetItem array does not go out of scope

Okay here the code snippet which may be of interest.



qDebug("inside updatelist");
quint8 qIndex;

ui->listWidget->setWrapping(false);
// ui->listWidget->clear();

qDebug()<<"total items = "<<tot_items;
for (qIndex = 0; qIndex < tot_items; ++qIndex)
{
qDebug("inside for loop");
QString item_val = QString(ItemsInList[qIndex].name).append("\n").append(QString("$").append(ItemsInList[qIndex].price));
qDebug()<<"list widget string to be populated = "<<item_val;
item[qIndex].setText(item_val);
QString icon = QString("/home/charvi123/QtSDK/Projects/scanIt/numbers/") + QString("%1").arg((qIndex + 1), 0, 10) + QString(".png");
item[qIndex].setIcon(QIcon(icon));
ui->listWidget->addItem(&item[qIndex]);
}
ui->listWidget->setFocus();




QListWidgetItem item[MAX_ITEMS];
typedef struct _Item_list_entity
{
QString name;
QString price;

}ListItemSingle;
ListItemSingle ItemsInList[ITEMS];

StrikeByte
13th August 2012, 10:25
Try using this:

The QListWidget destroys your array of QListWidgetItems during the clear (so the first time it's not a problem the second time is). this code generates new items when they are needed




qDebug("inside updatelist");
quint8 qIndex;

ui->listWidget->setWrapping(false);
// ui->listWidget->clear();

qDebug()<<"total items = "<<tot_items;
for (qIndex = 0; qIndex < tot_items; ++qIndex)
{
qDebug("inside for loop");
QString item_val = QString(ItemsInList[qIndex].name).append("\n").append(QString("$").append(ItemsInList[qIndex].price));
qDebug()<<"list widget string to be populated = "<<item_val;
QListWidgetItem * item = new QListWidgetItem;
item.setText(item_val);
QString icon = QString("/home/charvi123/QtSDK/Projects/scanIt/numbers/") + QString("%1").arg((qIndex + 1), 0, 10) + QString(".png");
item.setIcon(QIcon(icon));
ui->listWidget->addItem(item);
}
ui->listWidget->setFocus();



Question why do you want to save the data twice? once in the item array and once in the listwidget
For this i would use the QStandardItemModel in combination with the QListView

yeye_olive
13th August 2012, 10:35
You do double-delete your items.

On line 16 of the first code snippet, you add item[qIndex] to the list widget. When calling QListWidget::clear() (or when the widget is destroyed), the item at this memory address is deleted. Since it was not allocated on the heap but (presumably) on the stack or as a global variable in line 1 of the second code snippet, the underlying code to free() reports a corruption.

This is easy to fix: allocate the items on the heap (with new) and let the list widget delete them when appropriate.

Charvi
14th August 2012, 07:30
Oops ..
Correct. Thanks folks.