PDA

View Full Version : QTableView with QSortFilterProxyModel



TheGrimace
12th November 2012, 21:14
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:



QTableView* view = getTheTableView(); // psuedocode that gets member in this case
QModelIndexList indexes = view->selectionModel()->selectedIndexes();

//Iterate through the indexes and output them to the csv file


Is that the call I want?

I have also tried:



QSortFilterProxyModel* model = getTheModel(); //Psuedocode that gets member in this case
QTableView* view = getTheTableView(); // psuedocode that gets member in this case
QModelIndexList indexes = view->selectionModel()->selectedIndexes();

QItemSelection* itemSelection = model->mapSelectionFromSource(indexes); //Tried TO and FROM source here
QModelIndexList* mappedIndexes = itemSelection->indexes;

//try output on mappedIndexes



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.

norobro
13th November 2012, 04:11
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:
QSortFilterProxyModel* model = getTheModel(); //Psuedocode that gets member in this case
QTableView* view = getTheTableView(); // psuedocode that gets member in this case

for(int i=0; i< model->rowCount(); ++i){
QModelIndex index = model->index(i,0); // only one column :)
if(view->selectionModel()->isSelected(index))
// output to csv file
}

TheGrimace
13th November 2012, 13:53
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

norobro
13th November 2012, 15:03
Iterate QHeaderView::logicalIndex().

QModelIndex index = model->index(row, view->horizontalHeader()->logicalIndex(col));

TheGrimace
13th November 2012, 15:38
B E A utiful! That is exactly what was needed. Thank you very much.