Results 1 to 4 of 4

Thread: Selections in a TableView with a QItemSelectionModel

  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.

  2. #2
    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: Selections in a TableView with a QItemSelectionModel

    Why does the "selectionModel" variable hold? How and when did you initialise it?

  3. #3
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Selections in a TableView with a QItemSelectionModel

    I don't understand your 1st question?
    Quote Originally Posted by wysota View Post
    Why does the "selectionModel" variable hold? How and when did you initialise it?
    2nd question :
    Quote Originally Posted by wysota View Post
    How and when did you initialise it?
    I initialise it in the main program :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. DSTableModel *tableModel = new DSTableModel();
    4. DSSortFilterProxy *sortFilterProxy = new DSSortFilterProxy();
    5. DSTableView *tableView = new DSTableView(0);
    6. DSTableDelegate *tableDelegate = new DSTableDelegate();
    7. //here
    8. QItemSelectionModel *selectionModel = new QItemSelectionModel(tableModel);
    9.  
    10. //After that, I create my main window; I pass through the constructor the different adress
    11. //of MVC elements :
    12. DSMainWindow *mainWin = new DSMainWindow(tableModel,
    13. sortFilterProxy,
    14. tableView,
    15. tableDelegate,
    16. selectionModel);
    17. }
    To copy to clipboard, switch view to plain text mode 

    In the main window, each MVC element is a property class initialise in constructor with arguments writtent above.
    Qt Code:
    1. DSMainWindow::DSMainWindow(DSTableModel *_tableModel,
    2. DSSortFilterProxy *_sortFilterProxy,
    3. DSTableView * _tableView,
    4. DSTableDelegate *_tableDelegate,
    5. QItemSelectionModel *_selectionModel,
    6. QWidget *parent,
    7. Qt::WFlags flags)
    8. : QMainWindow(parent, flags)
    9. {
    10. //Initialisation of properties
    11. tableModel = _tableModel;;
    12. sortFilterProxy = _sortFilterProxy;
    13. tableView = _tableView;
    14. tableDelegate = _tableDelegate;
    15. selectionModel = _selectionModel;
    16.  
    17. //Initialisation of the model structure
    18. sortFilterProxy->setSourceModel(tableModel);
    19. tableView->setModel(sortFilterProxy);
    20. tableView->setItemDelegate(tableDelegate);
    21. tableView->setSelectionModel(selectionModel);
    22. //...
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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: Selections in a TableView with a QItemSelectionModel

    Quote Originally Posted by xelag View Post
    I don't understand your 1st question?
    Sorry, should be "what" instead of "why"


    2nd question :

    I initialise it in the main program :
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. // ...
    4. DSTableView *tableView = new DSTableView(0);
    5. // ...
    6. QItemSelectionModel *selectionModel = new QItemSelectionModel(tableModel);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //Initialisation of the model structure
    2. sortFilterProxy->setSourceModel(tableModel);
    3. tableView->setModel(sortFilterProxy);
    4. //...
    5. tableView->setSelectionModel(selectionModel);
    6. //...
    To copy to clipboard, switch view to plain text mode 
    How about:
    Qt Code:
    1. QTableView *view = new QTableView(...);
    2. tableView->setModel(sortFilterProxy);
    3. QItemSelectionModel = tableView->selectionModel(); // <-- use this one
    To copy to clipboard, switch view to plain text mode 

    You don't need to create your own selection model, the view already has one. The selection model will tell you about selections of the model which is set in the view - in your case the proxy model, so you need to map indexes to the source model using QAbstractProxyModel::mapToSource() if you want to operate on the model directly. But it is possible that you can do most operations by operating on the proxy instead - then you don't need to map indexes.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.