Results 1 to 5 of 5

Thread: Row index change when using QSortFilterProxyModel after sorting in QT4

  1. #1
    Join Date
    Feb 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Row index change when using QSortFilterProxyModel after sorting in QT4

    Hi everybody,

    I have a strange problem with QT's table model/view. I want to use subclass of QSortFilterProxyModel to implement sorting on a table. According to QT's reference, I do like this:
    Qt Code:
    1. MyDataList list;
    2. //initialize of list
    3. MyTableModel* model = new MyTableModel(list);
    4. MySortFilterProxyModel* proxyModel = new MySortFilterProxyModel;
    5. proxyModel->setSourceModel(model);
    To copy to clipboard, switch view to plain text mode 

    The table view's sortingEnabled property is set to be true. When I click the horizontal header of the table, sorting behaves correctly except the row index is no longer starting from 1 and increase by 1. For example, before sorting, the row index is 1,2,3 from top to bottom, and after sorting, row index becomes 2,3,1. It seems row index is fixed to the row, which is not my expectation. What I want is the table behaves just like Microsoft Excel, row index does not change when sorting. Same problem occurs with filtering.

    I didn't override any virtual function in MySortFilterProxyModel. If I don't use MySortFilterProxyModel and implement source model's sort() method, I can get the right behavior. I am not sure if it is a problem with QSortFilterProxyModel, or some mistake in my implementation. Any suggestion on this?

    Thank you very much!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Row index change when using QSortFilterProxyModel after sorting in QT4

    Then don't make the row index part of the data. Use the header e.g. QTableView::setVerticalHeader() of the view for that.

  3. #3
    Join Date
    Feb 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Row index change when using QSortFilterProxyModel after sorting in QT4

    Quote Originally Posted by Lykurg View Post
    Then don't make the row index part of the data. Use the header e.g. QTableView::setVerticalHeader() of the view for that.
    No, I didn't use any customized header widget, just the default vertical header of QTableView. Neither did I make row index part of model data. Everything here is defaulted. You can see the code I attached, building it in Qt Creator. Btw, I'm using QT 4.7.0.
    Attached Files Attached Files

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Row index change when using QSortFilterProxyModel after sorting in QT4

    Ok, then I had get you wrong. Sorry.

    The Solution for your problem is pretty simple:
    Qt Code:
    1. QVariant MySortFilterProxyModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. if (role != Qt::DisplayRole)
    4. return QVariant();
    5. if (orientation == Qt::Horizontal)
    6. return QSortFilterProxyModel::headerData(section, orientation, role);
    7. return section + 1;
    8. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 1st March 2011 at 08:45.

  5. #5
    Join Date
    Feb 2011
    Posts
    12
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Row index change when using QSortFilterProxyModel after sorting in QT4

    Your solution just solves my problem. Thank you very much!

    I wonder how operations perform when there's a proxy model attached to a source model. It seems that a method (virtual method declared in their parent) is first invoked in the proxy model. If we override the method and did not invoke its parent's one, corresponding method will not be invoked. Actually, it's the QSortFilterProxyModel::method() invokes the corresponding method in source model. Is it right?

Similar Threads

  1. DockWidgetArea change and layout direction change
    By mstegehu in forum Qt Programming
    Replies: 1
    Last Post: 21st February 2012, 22:24
  2. Replies: 0
    Last Post: 7th October 2010, 20:38
  3. Readline from a particular index
    By giusepped in forum Qt Programming
    Replies: 5
    Last Post: 30th April 2010, 21:58
  4. Get Row Index
    By wirasto in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2009, 14:57
  5. Get index of QValueListIterator
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 17th February 2006, 11:27

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.