PDA

View Full Version : QSortFilterProxyModel : sort columns



Kouillo
11th September 2012, 18:57
Hello.

I am looking for a good way to sort columns (not rows).

I use a QTableView to display a QAbstractTableModel subclass and I want to sort columns.
The class do not seem to have the right methods to do this and I cannot find information on the Internet...

I implement a qSort function and a custom compare to determine the order but I do not find how to change the column order. I tried tableview->horizontalHeader()->moveSection(..., ...) but it is not aware of the mapping in SortModel and only refer to visible rows...

Thanks for the help.

pkj
11th September 2012, 19:17
No, AFAIK, there is no out of the box way.
What you need is essentially a transposed matrix, kind of. So let us just imagine your model which does the usual sorting based on columns and call it mother model. Now write another model on top of it. In its data function, you take the row and column out, transpose it, and ask mother model for data(QAbstractItemModel::data(index(columnOfThisMo del, RowOfThisModel))). So you have kind of transposed the model.
Now when you connect with row clicked slot, you ask mother model to be sorted according, to column which is same as row. And in the same function emit dataChanged() for all columns in model.

Kouillo
12th September 2012, 09:17
Thanks for the answer.

If I understand what you mean, you advise me to build one other QSortFilterProxyModel above the current one, where I swap data from row to column ?
And How can I swap it again to display columns as columns and rows as rows ?

This seems very complicated for the simple sort I have to do. My QSortFilterProxyModel has some other operations and I fear to get some trouble with this method...

Does it exist any method to move columns in the QSortFilterProxyModel, and edit mapping ?

ChrisW67
12th September 2012, 09:32
Since you have a custom model anyway why not do the sorting there?

Kouillo
12th September 2012, 09:36
I do not find how to move columns in the QSortFilterProxyModel.
I already make a sort for the rows.



This seems very complicated for the simple sort I have to do. My QSortFilterProxyModel has some other operations and I fear to get some trouble with this method...

Does it exist any method to move columns in the QSortFilterProxyModel, and edit mapping ?

pkj
12th September 2012, 09:44
Nope, what I am suggesting that in the main model, you have the transposed matrix which you want to show. This is the one which operates on data. The next one on top of it merely transposes the transposed matrix and makes it as you want to see. It means making a few models. A model creation doesn't take too much time.
However, I think I am unable to express it clearly.
You can as well follow Chris advice and do the sorting in your model itself. At the row button click, get all the rows and their data and emit signals like columnsAboutToBeMoved(), columnsMoved, etc.