Results 1 to 7 of 7

Thread: model and itemselectionmodel, wrong item count

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    52
    Thanked 42 Times in 42 Posts

    Default model and itemselectionmodel, wrong item count

    Hello,
    I have problem with selection inside model/views (ListView and TableView). The problem is that I want to remove from QStandardItemModel selected rows, everything works fine, except that some selected items are not removed. I spend few days looking at the code/reading but couldn't find solution.

    So I do this:
    assign model and itemselectionmodel to views (for clarity sake i post only TableView code) like this:
    Qt Code:
    1. tableView->setModel( &model ); //setup model before show()
    2. tableView->setSelectionBehavior( QAbstractItemView::SelectRows );
    3. smodelTable = new QItemSelectionModel( &model );
    4. tableView->setSelectionModel( smodelTable ); //I use atm. two separate selectionmodels only to figure out the problem
    To copy to clipboard, switch view to plain text mode 
    then, after item are selected, I use this code to remove selected rows from model:

    Qt Code:
    1. void MainWindow::clearSelectedItemsSlot()
    2. {
    3. QModelIndexList selIndeModel;
    4.  
    5. if( listView->isVisible()){
    6. selIndeModel = smodelList->selectedIndexes(); //for listView
    7. }else{
    8. selIndeModel = smodelTable->selectedRows(); //for tableView
    9. }
    10.  
    11. for( int i = 0; i < selIndeModel.size(); ++i )
    12. {
    13. model.removeRow( selIndeModel[i].row() );
    14. fileNames.removeAt( selIndeModel[i].row() );
    15. }
    16.  
    17. selIndeModel.clear(); //for test purpose
    18. smodelList->clearSelection();
    19. smodelTable->clearSelection();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Items are indeed removed, but sometimes one stays in model, sometimes one in middle, depending what was selected.
    I can't think of any reason why this is happening.

    Any suggestion are more then welcome.
    PS> This behaviour is same for both listView and tableView, so I suspect that my remove code is wrong

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: model and itemselectionmodel, wrong item count

    The indexes in your selection model may be invalidated (or change what they point to) by calling removeRow() i.e. the row numbers change on rows later than than the one you remove. You want to remove the rows in descending row number order.

  3. #3
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    52
    Thanked 42 Times in 42 Posts

    Default Re: model and itemselectionmodel, wrong item count

    It gave me same behaviour,
    Qt Code:
    1. for( int i = selIndeModel.size()-1; i >= 0 ; --i )
    2. {
    3. model.removeRow( selIndeModel[i].row() );
    4. fileNames.removeAt( selIndeModel[i].row() );
    5. }
    To copy to clipboard, switch view to plain text mode 

    when deleting only last element it works well, but when selecting i.e. odds it leaves element or skip and remove next one.
    Last edited by Talei; 14th April 2010 at 03:45.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: model and itemselectionmodel, wrong item count

    The indexes on the QModelIndexList are not necessarily in row order, so reversing the order you extract them from the list is not the same as "descending row number order". Drop a:
    Qt Code:
    1. qDebug() << "removeRow()"<<iselIndeModel[i].row() << iselIndeModel[i].data().toString() ;
    To copy to clipboard, switch view to plain text mode 
    inside the loop to see what rows are being deleted.

    Build a QList<int> of row numbers, sort it, then run the loop.

    Alternatively, this may be a case where QPersistentModelIndex is useful. Convert your QModelIndexList to a QList<QPersistentModelIndex> and use that in the loop. Indexes should remain valid.

  5. The following user says thank you to ChrisW67 for this useful post:

    Talei (14th April 2010)

  6. #5
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    52
    Thanked 42 Times in 42 Posts

    Default Re: model and itemselectionmodel, wrong item count

    Thank You very much, I used PersistantModel and everything works fine, although I need to rethink about QStringList indexes removal.

    Qt Code:
    1. QModelIndexList selIndeModel;
    2. QList<QPersistentModelIndex> selPersistIndeModel;
    3.  
    4. if( listView->isVisible()){
    5. selIndeModel.append( smodelList->selectedIndexes() );
    6. }else{
    7. selIndeModel = smodelTable->selectedRows();
    8. }
    9.  
    10. //populate persistant indexes
    11. for( int i = 0; i < selIndeModel.count(); ++i)
    12. selPersistIndeModel.append( selIndeModel.at(i) );
    13.  
    14. for( int i = selPersistIndeModel.size()-1; i >= 0; --i )
    15. {
    16. //qDebug() << "removeRow()"<< selIndeModel[i].row();
    17. model.removeRow( selPersistIndeModel[i].row() );
    18. fileNames.removeAt( selPersistIndeModel[i].row() );
    19. }
    To copy to clipboard, switch view to plain text mode 
    And question, did I do it correctly or maybe there are some better way of ding it?

  7. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: model and itemselectionmodel, wrong item count

    It works... that's the main thing.

    You may be able to create selPersistIndeModel in one hit at line 5/7:
    Qt Code:
    1. selPersistIndeModel.append( smodelList->selectedIndexes() );
    To copy to clipboard, switch view to plain text mode 
    but I'm not sure if the compiler will complain or do an implicit conversion for you.

    There are probably more elegant methods, but someone else will have to point them out.

  8. #7
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    52
    Thanked 42 Times in 42 Posts

    Default Re: model and itemselectionmodel, wrong item count

    Yes I agree, but there is also fine line between working efficiently and not. I ask only because I try to learn "good programming practice".
    Also I had problem with QStringList so I implemented You previous suggestion, (QList<int>)

    Here is a code that deletes selected items from QStringList and removes rows from model

    Qt Code:
    1. void MainWindow::clearSelectedItemsSlot()
    2. {
    3. QModelIndexList selIndeModel;
    4.  
    5. //load selection indexes from table/list views
    6. if( listView->isVisible()){
    7. selIndeModel = smodelList->selectedIndexes();
    8. }else{
    9. selIndeModel = smodelTable->selectedRows();
    10. }
    11.  
    12. QList<int> lint;
    13. //populate lint
    14. for( int i = 0; i < selIndeModel.count(); ++i)
    15. lint.append( selIndeModel[i].row() );
    16.  
    17. //sort lint list and remove string at that pos from back
    18. //that way indexes don't shift
    19. qSort( lint.begin(), lint.end() );
    20.  
    21. for( int i = lint.size()-1; i >= 0; --i )
    22. {
    23. model.removeRow( lint[i] );
    24. fileNames.removeAt( lint[i] );
    25. }
    26.  
    27. smodelList->clearSelection();
    28. smodelTable->clearSelection();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thank you again for suggestion, I really appreciate it.
    Best regards

Similar Threads

  1. Custom model item deletion (Qt4.4)
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2008, 15:35
  2. Quick question about filtering item model
    By maverick_pol in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2007, 17:29
  3. Checkable item in tree model
    By zlatko in forum Qt Programming
    Replies: 1
    Last Post: 11th May 2007, 12:35
  4. Higlighting wrong item
    By Kapil in forum Qt Programming
    Replies: 5
    Last Post: 21st April 2006, 16:08
  5. Replies: 2
    Last Post: 16th February 2006, 20:09

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
  •  
Qt is a trademark of The Qt Company.