Results 1 to 3 of 3

Thread: Model sorting vs. selection

  1. #1
    Join Date
    Sep 2006
    Posts
    20
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question Re: QTableView versus QTableWidget

    Hello! After the sorting of my model (derived from the abstract table model, my own file model) the selection on my table view remains on the (now false) place. The problem: my selection is on the view and my sorting is in the model.
    I thought it should work automatically in the view to keep the selection of indexes and not of cells by sorting my model.
    What can You recommend?

    Qt Code:
    1. void FileModel::sort ( int column, Qt::SortOrder order )
    2. {
    3.  
    4. bool (*compare_funk)(const BUFileInfo &a,const BUFileInfo &b)=0; //point to the compare-function
    5. switch (column)
    6. {
    7. case 0:
    8. compare_funk=(order)?BUFileInfo::biggerName:BUFileInfo::lessName;
    9. break;
    10. case 1:
    11. compare_funk=(order)?BUFileInfo::biggerSize:BUFileInfo::lessSize;
    12. break;
    13. case 2:
    14. compare_funk=(order)?BUFileInfo::biggerExt:BUFileInfo::lessExt;
    15. break;
    16. case 3:
    17. compare_funk=(order)?BUFileInfo::biggerDate:BUFileInfo::lessDate;
    18. break;
    19. case 4:
    20. compare_funk=(order)?BUFileInfo::biggerState:BUFileInfo::lessState;
    21. break;
    22. }
    23.  
    24. if(compare_funk)
    25. {
    26. //std::list<BUFileInfo>::sort(fileList.begin(),fileList.end(),compare_funk);
    27. qSort(fileList.begin(),fileList.end(),compare_funk);
    28. emit layoutChanged();
    29.  
    30. }
    31.  
    32. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 14th October 2006 at 18:40. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableView versus QTableWidget

    Quote Originally Posted by VlJE View Post
    What can You recommend?
    I think Qt 4.2's QSortFilterProxyModel should be able to do what you want.
    J-P Nurmi

  3. #3
    Join Date
    Sep 2006
    Posts
    20
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Model sorting vs. selection

    I know QSortFilterProxyModel from 4.1.2. It really sorts and doesn't lose selection ( the current item can be lost) .
    The Problem is that I can overwrite the methode lessThen() but not biggerThen().
    I d'like to have the directory list above not dependent of the reverse flag.

    My (nor very good) solution was to marry the View and the model
    (

    class FileModel: public QAbstractTableModel
    {
    public:
    .....
    bool setSelectionModel(QItemSelectionModel *selectionModel)
    {
    pSelectionModel= selectionModel;
    return bool(selectionModel) ;
    }
    ......
    private:
    .....
    std::list<BUFileInfo> fileList;
    QItemSelectionModel *pSelectionModel;

    };
    )
    and save the selection positions in the data container at the beginning of my Sort methode
    (
    void FileModel::sort ( int column, Qt::SortOrder order )
    {
    //save the selection if the View is known
    QVector<const BUFileInfo *> fiSelVector;
    .........
    )
    and find the appropriate indexes and set the selection again after the sorting.

Similar Threads

  1. [SOLVED] QTreeView drawing selection with icons
    By Timewarp in forum Qt Programming
    Replies: 7
    Last Post: 7th February 2013, 07:52
  2. Sharing Selections between Model and ProxyModel
    By mentat in forum Qt Programming
    Replies: 14
    Last Post: 27th January 2010, 17:31
  3. hierarchical model in a flat view
    By gniking in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2009, 20:17
  4. QTableWidget Sorting Multiple Selection
    By rhiacasta in forum Qt Programming
    Replies: 1
    Last Post: 30th August 2006, 21:05
  5. QSortFilterProxyModel signal and selection confusion
    By pascal123 in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2006, 16:25

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.