Results 1 to 4 of 4

Thread: Selections in a TableView with a QItemSelectionModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question Selections in a TableView with a QItemSelectionModel

    Hello,

    I have a problem with a Model-View-Controler.

    I can't get the index of the selected items in my view. It seems that there are no connection between my model and my selection model

    Here is my MVC,
    • model : QAbstractTableModel
    • view : QTableView Proxy
    • proxy : QSortFilterProxyModel
    • delegate : QAbstractItemDelegate
    • selection model : QItemSelectionModel


    My view selection setting is set as :
    Qt Code:
    1. setSelectionMode (QAbstractItemView::ExtendedSelection);
    2. setSelectionBehavior (QAbstractItemView::SelectRows);
    To copy to clipboard, switch view to plain text mode 


    In my model, each data gets a Qt::ItemFlags as Qt::ItemIsSelectable :
    Qt Code:
    1. Qt::ItemFlags DSTableModel::flags(const QModelIndex &_index) const
    2. {
    3. if (!_index.isValid())
    4. {
    5. return QAbstractItemModel::flags(_index) | Qt::ItemIsEnabled;
    6. }
    7. return QAbstractItemModel::flags(_index) | Qt::ItemIsEditable | Qt::ItemIsSelectable;
    8. }
    To copy to clipboard, switch view to plain text mode 

    In my main program, i use this slot function to delete the selected rows in the view:
    Qt Code:
    1. void DSMainWindow::deleteDataSlot()
    2. {
    3. //Get the QModelIndex list of items selected in the view
    4. QModelIndexList tmpModelIndexList = selectionModel->selectedIndexes();
    5.  
    6. if(tmpModelIndexList.count()==0)
    7. {
    8. //---> Always comes here because no items have been detected selected, despite
    9. //the fact that the user has selected rows for real in the view !!!!!!!!!!!!!!!!!!
    10. QMessageBox::information(this, tr("DataSearch delete data"),
    11. tr("No data is selected"),
    12. QMessageBox::Ok,0,0);
    13. return;
    14. }
    15. else{}
    16.  
    17. int tmpMsgRet = QMessageBox::question(this, tr("DataSearch delete data"),
    18. tr("Are you sure you want to delete the selected data?") +
    19. tr("\nThe data deleted will be lost."),
    20. QMessageBox::Yes,QMessageBox::No | QMessageBox::Default,0);
    21.  
    22. if (tmpMsgRet == QMessageBox::Yes)
    23. {
    24. //list used to store the row already deleted (multiple QModelIndex for a row
    25. QList<int> tmpRowRemoved;
    26.  
    27. int currentRow;
    28.  
    29.  
    30. for(int i=0; i<tmpModelIndexList.count();i++)
    31. {
    32. currentRow = tmpModelIndexList.at(i).row();
    33.  
    34. //test if the row has not been deleted yet
    35. if(tmpRowRemoved.indexOf(currentRow) > 0)
    36. {
    37. //delete the row in the model
    38. tableModel->removeRows(currentRow,1,tableView->currentIndex());
    39. tmpRowRemoved.append(currentRow);
    40. }
    41. }
    42. }
    43. else{}
    44. }
    To copy to clipboard, switch view to plain text mode 

    Can the delegate or the proxy cause a problem? Is there something to add in the model?

    Please help. I'm searching for a long time where it bugs... But those MVCs are quite tough to debug...

    Thx,
    Xelag
    Last edited by xelag; 27th November 2006 at 00:36.

Similar Threads

  1. TreeView, TableView
    By rbrand in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2006, 08:54
  2. Modified tableview
    By dkite in forum Qt Programming
    Replies: 2
    Last Post: 14th March 2006, 01:58

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.