Results 1 to 11 of 11

Thread: Crash on removing a row

  1. #1
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Crash on removing a row

    Hi,

    I'm trying to remove a row from a QTableWidget using removeRow(...) and the application crashes. When I remove a row other than row 0, it usually doesn't crash, however removing row 0 always crashes.

    The application crashes with:
    ASSERT failure in QList<T>:perator[]: "index out of range", file c:/Qt/2009.03/qt/include/QtCore/../../src/corelib/tools/qlist.h, line 403

    I saw a similar situation on this forum, with no real solution.

    Any replies are appreciated. Thank you.

  2. #2
    Join Date
    Aug 2008
    Posts
    134
    Thanks
    10
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash on removing a row

    before removing the row check the current Index. Seems to be deleting the item which are not there in the list. Use currentRow() and check whether return value is -1 or 0. if return value is -1, then if u r trying to delete definitely application crashes

  3. #3
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Crash on removing a row

    can you post a little bit of your code?

  4. #4
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Crash on removing a row

    currentRow() is returning the correct value.

    Here's the code, triggered when a button is clicked. The row index is correct because I printed the value in the console. I even hardcoded the application to remove row index 0 and it still crashes.

    Qt Code:
    1. void MainWindow::on_removeButton_clicked()
    2. {
    3. QList<QTableWidgetItem *> selectedItems = ui->clipListWidget->selectedItems();
    4. if ((ui->clipListWidget->rowCount() > 0) && (selectedItems.count() > 0))
    5. {
    6. ui->clipListWidget->removeRow(selectedItems[0]->row());
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Crash on removing a row

    try to clear the selection of item first before removing

    Qt Code:
    1. selectedItems[0]->setSelected(false);
    2. ui->clipListWidget->removeRow(selectedItems[0]->row());
    To copy to clipboard, switch view to plain text mode 

    also make sure the you do not access the item anywhere else

    if you single step in debugger .. on which line it crashes?

  6. #6
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Crash on removing a row

    It crashes on:

    Qt Code:
    1. ui->clipListWidget->removeRow(selectedItems[0]->row());
    To copy to clipboard, switch view to plain text mode 

    Apparently its not letting me modify any properties of selected items. Not even unselecting selected items.

  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Crash on removing a row

    ok.. can you produce a minimal compilable example reproducing the problem?

  8. #8
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Crash on removing a row

    I've tried to reprodce it but it is not happening.

    I've now tried to unselect all items one after the other. All is fine until I unselect the last item. When I do, I get the 'index out of range' crash.

    EDIT: Turns out that the crash was caused in the SelectionChanged event. When the last row was being removed, the selection event was being triggered, but it wasn't handling the 'nothing selected' case correctly.
    Last edited by inktomi; 10th July 2009 at 09:54.

  9. #9
    Join Date
    Jul 2009
    Posts
    23
    Thanks
    2

    Default Re: Crash on removing a row

    Sorry for bothering you. Seems I still can't use the debugger correctly.

  10. #10
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Crash on removing a row

    I'm also have such problem - crash on deleting row = 0.
    Also read many topics and try different decisions,
    but nothing help me.

    But seems solutions is found,at least for me it sufficient:

    in QtDesigner -> property page of QTableWidget and set -> SelectionBehavior -> SelectRows
    Qt Code:
    1. tableNotifications->setSelectionBehavior(QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 
    due to this, now I call deleteRow(0) and all works! without crash.

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Crash on removing a row

    Be sure not to delete items from within slots connected to signals that have the item (or its index) as their parameter.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Crash: Heap corruption due to selectedRows()
    By Ankitha Varsha in forum Qt Programming
    Replies: 16
    Last Post: 1st October 2010, 00:55
  2. Replies: 28
    Last Post: 9th March 2010, 08:59
  3. QString comparison gives wierd crash
    By supergillis in forum Qt Programming
    Replies: 0
    Last Post: 1st June 2009, 22:09
  4. Replies: 2
    Last Post: 13th August 2008, 17:46
  5. QTimer ->start(0) + OpenGL + resize/move window => crash
    By anthibug in forum Qt Programming
    Replies: 5
    Last Post: 8th July 2008, 11:01

Tags for this Thread

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.