Results 1 to 7 of 7

Thread: RemoveRows

  1. #1
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default RemoveRows

    Hi,

    I want to remove rows in a QTreeView. I do that with:

    beginRemoveRows(parentIndex, row, erow);
    // erase data in the branch
    endRemoveRows();


    This is working as long the row to remove is not selected. If it's selected I get a access violation.

    It doesnt help moving the selected and current item to a neighboured one before removing.

  2. #2
    Join Date
    Jan 2006
    Location
    Sofia, Bulgaria
    Posts
    24
    Thanks
    1
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: RemoveRows

    if removing the selected row, you should do the code inside a shell like this:
    Qt Code:
    1. if ( item->isSelected() )
    2. QTimer::singleShot( 0, this, SLOT( my_slot_that_deletes_the_selected_item() ) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: RemoveRows

    Thank you, gadnio,

    I have tried, but have the same effect.
    Should the slot 'my_slot_that_deletes_the_selected_item()' contain the begin- and endRemoveRows or should this be done outside

    With putting in more checke in the parent() function of my model I most come through the deleting, but this is satisfying.

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

    Default Re: RemoveRows

    Could you show us the whole function used to remove rows and the context where it is used?
    I didn't have any trouble removing a selected item...

  5. #5
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: RemoveRows

    This is the Remove function:


    Qt Code:
    1. bool GeometryModel::removeRows(int row, int count, const QModelIndex &parent)
    2. {
    3. bool done = false;
    4. GeometryItem *parentItem;
    5.  
    6. if (parent.isValid()) {
    7. parentItem = static_cast<GeometryItem*>(parent.internalPointer());
    8. if (parentItem) {
    9. parentItem = parentItem->parent();
    10. if (parentItem) {
    11. QModelIndex parentIndex = parent.parent();
    12. int nc = parentItem->childCount();
    13. if (row < 0) row = 0;
    14. if ((row < nc) && (count > 0)) {
    15. int erow = row + count - 1;
    16. if (erow >= nc) erow = nc - 1;
    17. beginRemoveRows(parentIndex, row, erow);
    18. done = true;
    19. while (done && (row <= erow)) {
    20. selectedItem = parentItem;
    21. selectedRow = erow;
    22. done = parentItem->removeChild(erow);
    23. erow--;
    24. }
    25. endRemoveRows();
    26. }
    27. }
    28. }
    29. }
    30. return done;
    31. }
    To copy to clipboard, switch view to plain text mode 


    And this is how it's called:

    Qt Code:
    1. void TreeView::deleteSelectedItems()
    2. {
    3. QItemSelectionModel *selections = selectionModel();
    4. QModelIndexList selIndexes = selections->selectedIndexes();
    5. GeometryModel* gModel = (GeometryModel*)model();
    6.  
    7. void *ip_mom = NULL;
    8. int row_mom = -1;
    9.  
    10. for (int i = 0; i < selIndexes.count(); i++) {
    11. QModelIndex ri = selIndexes.at(i);
    12. if ((ip_mom != ri.internalPointer()) || (row_mom != ri.row())) {
    13. ip_mom = ri.internalPointer();
    14. row_mom = ri.row();
    15. gModel->removeRows(row_mom,1,ri);
    16. }
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 

    The code is working with not selected branches

    (This is my first QT-Project)

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

    Default Re: RemoveRows

    You have a little mess with all those parents and pointers

    First few questions:

    Qt Code:
    1. if (parent.isValid()) {
    To copy to clipboard, switch view to plain text mode 
    What is parent is not valid (which is true for top level model items)?

    Qt Code:
    1. parentItem = static_cast<GeometryItem*>(parent.internalPointer());
    2. if (parentItem) {
    To copy to clipboard, switch view to plain text mode 

    This will always be true (provided that you have a correct index() method in your model) but may be incorrect -- are you sure parent was not deleted before current item is deleted? While deleting an item you have to delete all its children first and not try to delete them after the parent item is deleted (so remove all references to them) and I don't see you do that in your code (and I guess it is hierarchical if you reference parent-child relation).

    Qt Code:
    1. int nc = parentItem->childCount();
    To copy to clipboard, switch view to plain text mode 
    if parentItem is invalid (and my guess is it is invalid) this will cause a segfault.

    My general hint is that you try to avoid all these references to parents and grandparents.

    I see another problem too:

    Qt Code:
    1. #
    2. QItemSelectionModel *selections = selectionModel();
    3. QModelIndexList selIndexes = selections->selectedIndexes();
    To copy to clipboard, switch view to plain text mode 

    The last call may return indexes which are selected, but they may be (I don't know, please check that) indexes from the selection model and not the base model! And you're using them as indexes from the base model:

    Qt Code:
    1. QModelIndex ri = selIndexes.at(i);
    2. //...
    3. gModel->removeRows(row_mom,1,ri);
    To copy to clipboard, switch view to plain text mode 

    You should build an index from the "gModel" from data obtained from the selection model.

  7. #7
    Join Date
    Jan 2006
    Posts
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: RemoveRows

    You were right with the bad mixing of selection and model indexes. I have cleared this.
    And I also have thrown the grandparents out of the family, my code is much shorter and looks better now.

    Nevertheless I still had a crash after deleting a selected branch and have solved this by not deleting the removed branches immediately, I have put them into a trash, which can be cleared later. That's no problem, I have to keep these items anyway for undo functions.

    After that I still came into a eternal loop after removing a top level subbranch, this I have solved by clearing the selection before removing.

    The whole thing seems to be stable now.

    Thank you very much for your suggestions.

    Best regards,

    Hans

Similar Threads

  1. QtSqlQueryModel removeRow's problem
    By qtman in forum Qt Programming
    Replies: 3
    Last Post: 26th February 2008, 11:28

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.