Results 1 to 6 of 6

Thread: How mantain a number of rows selected in a QTableView after the model is reset.

  1. #1
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default How mantain a number of rows selected in a QTableView after the model is reset.

    Hi,
    I have a QAbstracTableModel that provide data for a QTableView. From time to time my model is reset, so when the internal data is about to be changed I call beginResetModel() then I change the internal data and finally calls reset() and endResetModel().

    The form holding the respective view has the slotholdActiveFlightSelectio() connected to modelAboutToReset() and the slot resetActiveFlightSelection() connected to modelReset().
    When the slot triggered by modelAboutToReset() is called I basically store the callsign of a item.
    When the slot triggered by modelReset() is called I search for the callsign and then I try to select the correponding item at the new view.

    this is the code:
    Qt Code:
    1. void FlightTables::holdActiveFlightSelection()
    2. {
    3. int k;
    4. selectedActiveFlights.clear();
    5. QModelIndexList selectedRows = activeSelectionModel->selectedRows();
    6. if(selectedRows.size() == 0) return;
    7. for(k=0;k<selectedRows.size();k++)
    8. {
    9. selectedActiveFlights.append(activeModel->data(selectedRows.at(k),Qt::DisplayRole).toString());
    10. }
    11. }
    12.  
    13. void FlightTables::resetActiveFlightSelection()
    14. {
    15. int k;
    16. QString callsign;
    17. if(selectedActiveFlights.size() == 0) return;
    18. activeSelectionModel = ui->activeFlightTableView->selectionModel();
    19. for(k=0;k < activeModel->rowCount();k++)
    20. {
    21. QModelIndex tmpIndex = activeModel->index(k,0);
    22. QItemSelection selection;
    23. callsign = activeModel->data(tmpIndex,Qt::DisplayRole).toString();
    24. if(selectedActiveFlights.contains(callsign))
    25. {
    26. selection.select(tmpIndex,tmpIndex);
    27. activeSelectionModel->select(selection,QItemSelectionModel::Select|QItemSelectionModel::Rows);
    28. //activeSelectionModel->select(tmpIndex,QItemSelectionModel::Select|QItemSelectionmodel::Rows);
    29.  
    30. }
    31. }
    32. }
    To copy to clipboard, switch view to plain text mode 
    I am holding the callsigns before the model reset and searching for them in the new model, but when i try to paint the selecion at the QTableView by activeSelectionModel nothing happens?!
    I am not sure about the diference of calling:
    void QItemSelectionModel::select ( const QModelIndex & index, QItemSelectionModel::SelectionFlags command )
    or
    void QItemSelectionModel::select ( const QItemSelection & selection, QItemSelectionModel::SelectionFlags command )

  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: How mantain a number of rows selected in a QTableView after the model is reset.

    The easiest thing would be not to reset your model but instead add/remove/move rows.

    How exactly do you "try to paint the selection"?
    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.


  3. #3
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How mantain a number of rows selected in a QTableView after the model is reset.

    Quote Originally Posted by wysota View Post
    The easiest thing would be not to reset your model but instead add/remove/move rows.
    All data from the model changes every 1 minute so I beleive reseting it and only keeping track of the selected item would be better.

    Quote Originally Posted by wysota View Post
    How exactly do you "try to paint the selection"?
    I thought
    Qt Code:
    1. activeSelectionModel->select(selection,QItemSelectionModel::Select|QItemSelectionModel::Rows);
    To copy to clipboard, switch view to plain text mode 
    would do the trick.
    How can I select items in a QTableView programatically!

  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: How mantain a number of rows selected in a QTableView after the model is reset.

    Quote Originally Posted by ferrabras View Post
    All data from the model changes every 1 minute so I beleive reseting it and only keeping track of the selected item would be better.
    This is contradictory. If all items are removed from the model, there is no selection to track as there are no items that used to be in the model and could have been selected before the update. If some of the items stay in the model then you shouldn't be resetting the model but rather updating (adding/removing/updating rows) it.

    And yes, using select() should have worked assuming your stored selection list is valid and consistent with contents of the model.
    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.


  5. #5
    Join Date
    Dec 2009
    Posts
    8
    Thanks
    1
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How mantain a number of rows selected in a QTableView after the model is reset.

    The data is not removed but most of columns change, I would have to emit datachanged for each one of them, wouldn't I?
    Could you give an exemple or direct me to one!? Please!

  6. #6
    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: How mantain a number of rows selected in a QTableView after the model is reset.

    Quote Originally Posted by ferrabras View Post
    The data is not removed but most of columns change, I would have to emit datachanged for each one of them, wouldn't I?
    You can emit one signal for the whole model.
    Qt Code:
    1. emit dataChanged(index(0,0), index(rowCount()-1, columnCount()-1);
    To copy to clipboard, switch view to plain text mode 
    or something similar.
    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. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Count the number of rows in a QTableView
    By grub87 in forum Qt Programming
    Replies: 9
    Last Post: 28th June 2009, 16:31
  3. QTableView has constant number of rows
    By tomeks in forum Qt Programming
    Replies: 5
    Last Post: 10th December 2008, 15:29
  4. Model-View: how to get the data from my selected rows
    By viasant in forum Qt Programming
    Replies: 5
    Last Post: 11th August 2008, 02:16
  5. Model/View Confusion in QTreeView - number of rows
    By themolecule in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2007, 22:45

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.