Results 1 to 4 of 4

Thread: Show selected rows

  1. #1
    Join Date
    Apr 2009
    Posts
    32
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Show selected rows

    Dear Gurus,

    Suppose I have a table model that I view in a QTableView (A). In another QTableView (B) I want to display the rows that are selected in the first QTableView (A).

    How do I achieve this? Ie, each selected row in view (A) should be displayed in view (B).

    Best regards,

    Mads

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Show selected rows

    I haven't tried this yet (but I need to, since I need similar code too), but I would try this approach:

    Use viewA>selectedIndexes() to return a QModelIndexList
    Then a QFilterProxyModel to translate the viewA indexes to the modelA indexes.
    Then get the selected data with modelA->data() - returns QVariant
    Then insert the data into modelB using insertRows
    Then modelB->select()

    Good luck!

  3. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Show selected rows

    Also, check out QItemSelectionModel

  4. #4
    Join Date
    Feb 2010
    Location
    Sydney, Australia
    Posts
    111
    Thanks
    18
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Show selected rows

    You shouldn't need to have two models as the underlying data is the same for both TreeViews. This is one of the massive benefits of the MV(C) implementation of the Qt Model/View framework. It should definitely be possible to feed selected model indexes from one view to another using a QSortFilterProxy on the second to filter out the indexes that aren't required or rather filter in the nidexes that are required.

    I imagine you would start by getting the selected indexes of the treeViewA using a QItemSelectionModel:

    Qt Code:
    1. QItemSelectionModel* selectionModel = tableViewA->selectionModel();
    To copy to clipboard, switch view to plain text mode 

    Then handle treeViewA::selectionChanged(const QItemSelection & selected, const QItemSelection & deselected) which will be emitted whenever the selection in treeViewA changes (obviously!) and do one of these:

    Qt Code:
    1. QModelIndexList indexList = selectionModel->selectedIndexes()
    2. // OR
    3. QModelIndexList indexList = selectionModel->selectedRows()
    4. // OR
    5. QModelIndexList indexList = selectionModel->selectedColums()
    To copy to clipboard, switch view to plain text mode 

    Problem comes in knowing what to do with that list of selected indexes. I was hoping that you would be able to somehow get a QSortFilterProxyModel to filter based on a collection of indexes. I've just had a look at the documentation and it's not obvious how to do that. If I come up with anything, I'll post.

Similar Threads

  1. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 13:49
  2. removing of selected rows
    By MrShahi in forum Qt Programming
    Replies: 4
    Last Post: 10th June 2008, 16:05
  3. iterating selected rows in a qtableview
    By JeanC in forum Qt Programming
    Replies: 2
    Last Post: 19th January 2008, 15:29
  4. Showing selected rows in a separate table
    By dnnc in forum Qt Programming
    Replies: 3
    Last Post: 21st June 2007, 17:35
  5. Get list of selected rows from QTableView
    By jnk5y in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 17:59

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.