Results 1 to 6 of 6

Thread: QSortFilterProxyModel : sort columns

  1. #1
    Join Date
    Feb 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSortFilterProxyModel : sort columns

    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.

  2. #2
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel : sort columns

    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.

  3. #3
    Join Date
    Feb 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel : sort columns

    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 ?
    Last edited by Kouillo; 12th September 2012 at 08:32.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QSortFilterProxyModel : sort columns

    Since you have a custom model anyway why not do the sorting there?

  5. #5
    Join Date
    Feb 2012
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel : sort columns

    I do not find how to move columns in the QSortFilterProxyModel.
    I already make a sort for the rows.


    Quote Originally Posted by Kouillo View Post
    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 ?

  6. #6
    Join Date
    Feb 2011
    Location
    Bangalore
    Posts
    207
    Thanks
    20
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel : sort columns

    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.

Similar Threads

  1. customize a QSortFilterProxyModel with additional columns
    By winkle99 in forum Qt Programming
    Replies: 1
    Last Post: 16th November 2011, 04:17
  2. Sort huge data use QSortFilterProxyModel
    By jiaorenjie in forum Qt Programming
    Replies: 0
    Last Post: 16th March 2011, 02:56
  3. How to sort columns by "icon"?
    By youkai in forum Qt Programming
    Replies: 3
    Last Post: 31st July 2008, 16:25
  4. QSortFilterProxyModel - always sort 0 to the bottom
    By ironstorm in forum Qt Programming
    Replies: 2
    Last Post: 1st March 2008, 20:38
  5. QSortFilterProxyModel - sort strings
    By DPinLV in forum Qt Programming
    Replies: 3
    Last Post: 29th August 2006, 01:06

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.