Results 1 to 3 of 3

Thread: QTableView sorting when using a model

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QTableView sorting when using a model

    Hi,

    Having to do some modifications to some code someone did well over 10 months ago. Part of this is to sort some data in ascending order within a QTableView. Now I noticed it uses a model ( QAbstractTableModel ). The data seems to be stored within a QList. The objects that are stored in this QList are of the following :

    Qt Code:
    1. class ASignal
    2. {
    3. public:
    4. ASignal() { }
    5. char name[256];
    6. char unit[256];
    7. char strECU[20];
    8. double dmaximum;
    9. double dminimum;
    10. double dfactor;
    11. double doffset;
    12. UINT istartbit;
    13. UINT ilength;
    14. double realvalue;
    15. long rawvalue;
    16. double byteorder;
    17. };
    To copy to clipboard, switch view to plain text mode 

    I want to sort this QList<ASignal*> on the member name. What is the best and quickest way of achieving this? Before the data is inserted into the QList should I do some checking and use swap etc?

    Kind regards,
    Steve

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView sorting when using a model

    Use the three parameter version of qSort from QtAlgorithms.

  3. #3
    Join Date
    Apr 2006
    Location
    Minsk, Belarus
    Posts
    33
    Thanks
    2
    Thanked 6 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTableView sorting when using a model

    I would suggest not sorting data in model if you need only to display sorted data in QTableView.

    There is another good solution: use QSortFilterProxyModel as a proxy between your model and QTableView. If you want to have some specific sorting then you only need to re-implement one virtual method

    bool QSortFilterProxyModel::lessThan

    But in most case its default implementation will be enough (see Qt help & examples on QSortFilterProxyModel)

Similar Threads

  1. Replies: 6
    Last Post: 22nd November 2011, 03:53
  2. QTableView sorting
    By gabriels in forum Qt Programming
    Replies: 11
    Last Post: 6th October 2010, 17:13
  3. Help choosing a model type for QTableView
    By goes2bob in forum Newbie
    Replies: 1
    Last Post: 10th March 2008, 02:22
  4. Replies: 9
    Last Post: 7th November 2006, 15:10
  5. Model sorting vs. selection
    By VlJE in forum Qt Programming
    Replies: 2
    Last Post: 25th October 2006, 16:46

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.