Results 1 to 8 of 8

Thread: QListWidget->clear()

  1. #1
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default QListWidget->clear()

    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.

  2. #2
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget->clear()

    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().

  3. #3
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListWidget->clear()

    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 ?

  4. #4
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget->clear()

    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.

  5. #5
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListWidget->clear()

    - My QListWidgetItem array does not go out of scope

    Okay here the code snippet which may be of interest.

    Qt Code:
    1. qDebug("inside updatelist");
    2. quint8 qIndex;
    3.  
    4. ui->listWidget->setWrapping(false);
    5. // ui->listWidget->clear();
    6.  
    7. qDebug()<<"total items = "<<tot_items;
    8. for (qIndex = 0; qIndex < tot_items; ++qIndex)
    9. {
    10. qDebug("inside for loop");
    11. QString item_val = QString(ItemsInList[qIndex].name).append("\n").append(QString("$").append(ItemsInList[qIndex].price));
    12. qDebug()<<"list widget string to be populated = "<<item_val;
    13. item[qIndex].setText(item_val);
    14. QString icon = QString("/home/charvi123/QtSDK/Projects/scanIt/numbers/") + QString("%1").arg((qIndex + 1), 0, 10) + QString(".png");
    15. item[qIndex].setIcon(QIcon(icon));
    16. ui->listWidget->addItem(&item[qIndex]);
    17. }
    18. ui->listWidget->setFocus();
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QListWidgetItem item[MAX_ITEMS];
    2. typedef struct _Item_list_entity
    3. {
    4. QString name;
    5. QString price;
    6.  
    7. }ListItemSingle;
    8. ListItemSingle ItemsInList[ITEMS];
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2012
    Posts
    136
    Thanks
    2
    Thanked 27 Times in 24 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget->clear()

    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

    Qt Code:
    1. qDebug("inside updatelist");
    2. quint8 qIndex;
    3.  
    4. ui->listWidget->setWrapping(false);
    5. // ui->listWidget->clear();
    6.  
    7. qDebug()<<"total items = "<<tot_items;
    8. for (qIndex = 0; qIndex < tot_items; ++qIndex)
    9. {
    10. qDebug("inside for loop");
    11. QString item_val = QString(ItemsInList[qIndex].name).append("\n").append(QString("$").append(ItemsInList[qIndex].price));
    12. qDebug()<<"list widget string to be populated = "<<item_val;
    13. item.setText(item_val);
    14. QString icon = QString("/home/charvi123/QtSDK/Projects/scanIt/numbers/") + QString("%1").arg((qIndex + 1), 0, 10) + QString(".png");
    15. item.setIcon(QIcon(icon));
    16. ui->listWidget->addItem(item);
    17. }
    18. ui->listWidget->setFocus();
    To copy to clipboard, switch view to plain text mode 

    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
    Last edited by StrikeByte; 13th August 2012 at 10:34.

  7. #7
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QListWidget->clear()

    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.

  8. #8
    Join Date
    Mar 2012
    Location
    India
    Posts
    102
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: QListWidget->clear()

    Oops ..
    Correct. Thanks folks.

Similar Threads

  1. clear all QLineEdit
    By jaca in forum Qt Programming
    Replies: 7
    Last Post: 5th August 2016, 07:06
  2. Replies: 2
    Last Post: 1st April 2011, 09:32
  3. How to use the clear buttom
    By levorn10 in forum Newbie
    Replies: 2
    Last Post: 7th January 2011, 08:00
  4. QListWidget - clear()
    By Carlsberg in forum Qt Programming
    Replies: 3
    Last Post: 3rd June 2010, 07:01
  5. QTreeWidget->clear()?
    By vishal.chauhan in forum Qt Programming
    Replies: 20
    Last Post: 5th October 2007, 07:00

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.