Results 1 to 3 of 3

Thread: QSortFilterProxyModel - always sort 0 to the bottom

  1. #1
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post

    Default QSortFilterProxyModel - always sort 0 to the bottom

    I'm sort of stuck, I need to implement a sort for numbers which always places 0 values at the bottom of the sort (0 represents an uninitialized data value)...

    i.e.
    0,1,2,3
    Ascending -> 1,2,3,0
    Descending -> 3,2,1,0

    The only way I can think of to solve it is to somehow add a condition (to lines #10 and #11) based on the sort direction, I don't know how to tell what the sort direction is from within the context of my lessThan method.

    Can anyone suggest and approach for this either getting the sort direction or somehow getting 0 to sort lower (maybe doing something in FilterAcceptRows???)...

    Here's my code:

    Qt Code:
    1. bool SortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
    2. {
    3. // Do the strings look like they are BPM counts?
    4. const QString bpmPattern = "^[0-9]+\\.[0-9]+$";
    5. if (sourceModel()->data( left ).toString().trimmed().indexOf(QRegExp(bpmPattern)) == 0
    6. && sourceModel()->data( right ).toString().trimmed().indexOf(QRegExp(bpmPattern)) == 0) {
    7. double leftBPM = sourceModel()->data( left ).toString().trimmed().toDouble();
    8. double rightBPM = sourceModel()->data( right ).toString().trimmed().toDouble();
    9. qDebug() << "BPM Comparasion leftBPM =" << leftBPM << "rightBPM =" << rightBPM;
    10. if (leftBPM == 0.0) rightBPM = -1 * rightBPM;
    11. if (rightBPM == 0.0) leftBPM = -1 * leftBPM;
    12. return leftBPM < rightBPM;
    13. }
    14. ....
    To copy to clipboard, switch view to plain text mode 
    Last edited by ironstorm; 29th February 2008 at 17:02.

  2. #2
    Join Date
    Feb 2008
    Posts
    6
    Thanked 1 Time in 1 Post

    Default Re: QSortFilterProxyModel - always sort 0 to the bottom

    Here's the final working solution... Line 3 is the key... our parent class has a handle to a QTableView (m_pTrackTableView)
    Qt Code:
    1. bool SortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
    2. {
    3. const Qt::SortOrder sortOrder = ((MixxxView *)this->parent())->m_pTrackTableView->horizontalHeader()->sortIndicatorOrder();
    4.  
    5. // Do the strings look like they are BPM counts?
    6. const QString bpmPattern = "^[0-9]+\\.[0-9]+$";
    7. if (sourceModel()->data( left ).toString().trimmed().indexOf(QRegExp(bpmPattern)) == 0
    8. && sourceModel()->data( right ).toString().trimmed().indexOf(QRegExp(bpmPattern)) == 0) {
    9. double leftBPM = sourceModel()->data( left ).toString().trimmed().toDouble();
    10. double rightBPM = sourceModel()->data( right ).toString().trimmed().toDouble();
    11.  
    12. if (leftBPM == 0.0 && sortOrder == Qt::AscendingOrder) rightBPM = -1 * rightBPM;
    13. if (rightBPM == 0.0 && sortOrder == Qt::AscendingOrder) leftBPM = -1 * leftBPM;
    14. // qDebug() << "BPM Comparasion leftBPM =" << leftBPM << "rightBPM =" << rightBPM;
    15. return leftBPM < rightBPM;
    16. }
    17. ....
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSortFilterProxyModel - always sort 0 to the bottom

    I'm not sure this should be done in the sort filter proxy model. You could overload QAbstractItemModel::sort() either for your model or an abstract proxy model and have all the required parameters at hand.

Similar Threads

  1. Replies: 7
    Last Post: 15th November 2007, 17:19
  2. How to sort the treeWidget item according to size and date
    By santosh.kumar in forum Qt Programming
    Replies: 3
    Last Post: 23rd October 2007, 10:32
  3. Replies: 1
    Last Post: 11th September 2007, 13:34
  4. How to sort a QComboBox in Qt4
    By guenthk in forum Qt Programming
    Replies: 7
    Last Post: 25th September 2006, 18:35
  5. Keeping focus at bottom of QListView
    By jakamph in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2006, 14:45

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.