Results 1 to 4 of 4

Thread: Delete last item in QListWidget, application crash....!

  1. #1
    Join Date
    Mar 2010
    Location
    China
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Question Delete last item in QListWidget, application crash....!

    Hi all,
    i'm new to QT, I get application crashed when i delete the last item in QListWidget....
    following is my code...

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MainWindow : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit MainWindow(QWidget *parent = 0)
    8. : QWidget(parent)
    9. {
    10. _listWidget = new QListWidget(this);
    11. _listWidget->resize(300,200);
    12. _textEdit = new QTextEdit(this);
    13. _textEdit->move(0,240);
    14.  
    15. for(int i= 0 ; i < 3 ; i++){
    16. QListWidgetItem *item = new QListWidgetItem(_listWidget);
    17. item->setData(Qt::DisplayRole, tr("item %1").arg(i));
    18. item->setData(Qt::DecorationRole, qApp->style()->standardIcon(QStyle::SP_DirIcon));
    19. }
    20.  
    21. connect(_listWidget, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
    22. this, SLOT(listItemChged(QListWidgetItem*,QListWidgetItem*)));
    23.  
    24. QPushButton *delBtn = new QPushButton(tr("Delete current item"), this);
    25. delBtn->move(0, 210);
    26. connect(delBtn, SIGNAL(clicked()), this, SLOT(delCurrItem()));
    27.  
    28. resize(600,700);
    29. }
    30.  
    31.  
    32. private slots:
    33. void listItemChged(QListWidgetItem* currItem, QListWidgetItem*)
    34. {
    35. _textEdit->append(currItem->data(Qt::DisplayRole).toString());
    36. }
    37.  
    38. void delCurrItem()
    39. {
    40. if(_listWidget->count() < 1) return;
    41. delete _listWidget->currentItem();
    42. }
    43.  
    44. private:
    45. QListWidget *_listWidget;
    46. QTextEdit *_textEdit;
    47.  
    48. };
    49.  
    50. #include "main.moc"
    51.  
    52. int main(int argc, char *argv[])
    53. {
    54. QApplication app(argc, argv);
    55. MainWindow mainWin;
    56. mainWin.show();
    57. return app.exec();
    58. }
    To copy to clipboard, switch view to plain text mode 

    Could somebody help me? thanks in advance....
    Sorry for my poor English....

  2. #2
    Join Date
    Jun 2010
    Location
    Salatiga, Indonesia
    Posts
    160
    Thanks
    11
    Thanked 32 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    1

    Default Re: Delete last item in QListWidget, application crash....!

    When you delete the last item, in this function

    Qt Code:
    1. void listItemChged(QListWidgetItem* currItem, QListWidgetItem*)
    2. {
    3. _textEdit->append(currItem->data(Qt::DisplayRole).toString());
    4. }
    To copy to clipboard, switch view to plain text mode 

    currItem points to null (0). So, you need to check currItem value before accessing it.

    Qt Code:
    1. if(currItem != 0)
    2. _textEdit->append(currItem->data(Qt::DisplayRole).toString());
    To copy to clipboard, switch view to plain text mode 
    Last edited by saa7_go; 21st July 2010 at 05:37. Reason: updated contents

  3. The following user says thank you to saa7_go for this useful post:

    radory (21st July 2010)

  4. #3
    Join Date
    Mar 2010
    Location
    China
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: Delete last item in QListWidget, application crash....!

    Quote Originally Posted by saa7_go View Post
    When you delete the last item, in this function

    Qt Code:
    1. void listItemChged(QListWidgetItem* currItem, QListWidgetItem*)
    2. {
    3. _textEdit->append(currItem->data(Qt::DisplayRole).toString());
    4. }
    To copy to clipboard, switch view to plain text mode 



    currItem points to null (0). So, you need to check currItem value before accessing it.

    Qt Code:
    1. if(currItem != 0)
    2. _textEdit->append(currItem->data(Qt::DisplayRole).toString());
    To copy to clipboard, switch view to plain text mode 
    It's works now ....
    Thanks a lot....

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Delete last item in QListWidget, application crash....!

    Would suggest to use -
    delete QListWidget::takeItem(index);

    instead of delete QListWidget::currentItem;
    You are simply deleting that item, without removing it from the list widget. So list widget is bound to hold an invalid pointer !

    Also would advise to use QListWidget::itemActivated instead of currentItemChanged. Since itemActivated will be called only when user presses the item, and thats what you want I guess

Similar Threads

  1. delete runtime-created widgets causes crash
    By jonasbalmer in forum Qt Programming
    Replies: 5
    Last Post: 10th February 2010, 22:36
  2. delete QListWidgetItem in QListWidget
    By sophister in forum Qt Programming
    Replies: 1
    Last Post: 10th May 2009, 16:52
  3. How to use QMap::remove() to delete some item?
    By jedychen in forum Qt Programming
    Replies: 5
    Last Post: 18th September 2008, 08:29
  4. crash when delete QTreeWidgetItem*
    By evgenM in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 21:04
  5. QListWidget + Delete Key
    By bpetty in forum Newbie
    Replies: 5
    Last Post: 16th August 2006, 21:38

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.