Results 1 to 5 of 5

Thread: QTableView with QSortFilterProxyModel

  1. #1
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Question QTableView with QSortFilterProxyModel

    I have a QTableView with an overloaded QSortFilterProxyModel (filterAcceptsRows is the only overloaded method)

    I am trying to update this code not design it so I am coming into it and trying to figure out what is going on.

    The issue I am having is I am trying to output the selection from the table as a Comma Separated Value table for use in Excel and in the same order as it is visible. This is what I tried in pseudocode:

    Qt Code:
    1. QTableView* view = getTheTableView(); // psuedocode that gets member in this case
    2. QModelIndexList indexes = view->selectionModel()->selectedIndexes();
    3.  
    4. //Iterate through the indexes and output them to the csv file
    To copy to clipboard, switch view to plain text mode 

    Is that the call I want?

    I have also tried:

    Qt Code:
    1. QSortFilterProxyModel* model = getTheModel(); //Psuedocode that gets member in this case
    2. QTableView* view = getTheTableView(); // psuedocode that gets member in this case
    3. QModelIndexList indexes = view->selectionModel()->selectedIndexes();
    4.  
    5. QItemSelection* itemSelection = model->mapSelectionFromSource(indexes); //Tried TO and FROM source here
    6. QModelIndexList* mappedIndexes = itemSelection->indexes;
    7.  
    8. //try output on mappedIndexes
    To copy to clipboard, switch view to plain text mode 

    In all of these cases the QModelIndexList seems to stay in the same order.

    I am just trying to get the items as they are listed in the view.

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableView with QSortFilterProxyModel

    To maintain the order in the view, you'll have to iterate through the proxy model and check each index to see if it is selected.

    Using your example:
    Qt Code:
    1. QSortFilterProxyModel* model = getTheModel(); //Psuedocode that gets member in this case
    2. QTableView* view = getTheTableView(); // psuedocode that gets member in this case
    3.  
    4. for(int i=0; i< model->rowCount(); ++i){
    5. QModelIndex index = model->index(i,0); // only one column :)
    6. if(view->selectionModel()->isSelected(index))
    7. // output to csv file
    8. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to norobro for this useful post:

    TheGrimace (13th November 2012)

  4. #3
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with QSortFilterProxyModel

    Trying that now, Thanks. Just realized I forgot to mention that the columns are movable in the view and I need to maintain that order in the csv. That may affect the answer.

    Edit:

    Yes, this gets me the data, but unfortunately it is in the original order of the model and not in the revised view order.

    If I start with:

    1 2 3
    A B C
    X Y Z


    and move the columns in the view to give me: (moved last column to first)

    3 1 2
    C A B
    Z X Y

    How can I get THAT order? Since a model can have multiple views I am assuming that the model does not know the order, but I am not sure how to get it from the QTableView.

    Thanks
    Last edited by TheGrimace; 13th November 2012 at 14:42.

  5. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QTableView with QSortFilterProxyModel

    Iterate QHeaderView::logicalIndex().
    Qt Code:
    1. QModelIndex index = model->index(row, view->horizontalHeader()->logicalIndex(col));
    To copy to clipboard, switch view to plain text mode 

  6. The following user says thank you to norobro for this useful post:

    TheGrimace (13th November 2012)

  7. #5
    Join Date
    May 2007
    Posts
    90
    Thanks
    40
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableView with QSortFilterProxyModel

    B E A utiful! That is exactly what was needed. Thank you very much.

Similar Threads

  1. Problem with QTableView and QSortFilterProxyModel
    By unix7777 in forum Qt Programming
    Replies: 7
    Last Post: 24th August 2012, 08:11
  2. Moving Items in QTableView with QSortFilterProxyModel
    By Rudolf Rendier in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2012, 15:07
  3. Replies: 6
    Last Post: 22nd November 2011, 03:53
  4. Replies: 1
    Last Post: 18th November 2009, 23:21

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.