Results 1 to 9 of 9

Thread: QSortFilterProxyModel lessThan is never called.

  1. #1
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default QSortFilterProxyModel lessThan is never called.

    Hi,
    I have a subclass QSortFilterProxyModel with custom lessThan function to provide sort by specific columns but this function is never called. Maybe You know why?


    Qt Code:
    1. #ifndef CUSTOMSORTFILTERPROXYMODEL_H
    2. #define CUSTOMSORTFILTERPROXYMODEL_H
    3.  
    4. #include <QObject>
    5. #include <QSortFilterProxyModel>
    6. #include <QDebug>
    7.  
    8. class CustomSortFilterProxyModel : public QSortFilterProxyModel
    9. {
    10. Q_OBJECT
    11.  
    12. enum Column {
    13. Title,
    14. Authors,
    15. ReleaseDate,
    16. About,
    17. Format,
    18. Image
    19. };
    20.  
    21. public:
    22. CustomSortFilterProxyModel();
    23. ~CustomSortFilterProxyModel();
    24.  
    25. protected:
    26. bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
    27.  
    28. private:
    29. QList< Column > selectedColumns;
    30. };
    31.  
    32. #endif // CUSTOMSORTFILTERPROXYMODEL_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "customsortfilterproxymodel.h"
    2.  
    3. CustomSortFilterProxyModel::CustomSortFilterProxyModel()
    4. {
    5.  
    6. }
    7.  
    8. CustomSortFilterProxyModel::~CustomSortFilterProxyModel()
    9. {
    10.  
    11. }
    12.  
    13. bool CustomSortFilterProxyModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
    14. {
    15. // never called, why?
    16. qDebug()<< "Custom Sort Filter Proxy Model :: lessThan";
    17. qDebug()<< "colums size: " << selectedColumns.size();
    18.  
    19. return false;
    20. }
    To copy to clipboard, switch view to plain text mode 

    and in mainwindow
    Qt Code:
    1. proxyModel.setSourceModel( &customTableModel );
    2. ui->tableView->setModel( &proxyModel );
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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 lessThan is never called.

    The customTableModel and proxyModel are both stack allocated by the look of it. Are both objects staying in scope for the life of the view?

  3. #3
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    The customTableModel and proxyModel are both stack allocated by the look of it. Are both objects staying in scope for the life of the view?
    Yes, this is my MainWindow .h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QFile>
    6. #include <QProgressDialog>
    7. #include <QDesktopServices>
    8. #include <QSortFilterProxyModel>
    9.  
    10. #include <QDebug>
    11. #include <customtablemodel.h>
    12. #include <inputdialog.h>
    13. #include <sortdialog.h>
    14.  
    15. namespace Ui {
    16. class MainWindow;
    17. }
    18.  
    19. class MainWindow : public QMainWindow
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit MainWindow(QWidget *parent = 0);
    25. ~MainWindow();
    26.  
    27. public slots:
    28. void insertItem();
    29. void removeItem();
    30. void editItem( const QModelIndex & index );
    31.  
    32. void sort();
    33.  
    34. void about();
    35.  
    36.  
    37. private:
    38. Ui::MainWindow *ui;
    39. CustomTableModel customTableModel;
    40.  
    41. };
    42.  
    43. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Other functions from QSortFilterProxyModel like setFilterRegExp() works properly.
    And sort by column

    Qt Code:
    1. proxyModel.sort( 0 );
    To copy to clipboard, switch view to plain text mode 

    too, but lessThan is never called and I do not know why. When this function should be called?

    Important - I use QSortFilterProxyModel with my custom model class, which inherits QAbstractTableModel so maybe I should implement some specific functions? This is my custom model class


    Qt Code:
    1. #ifndef CUSTOMTABLEMODEL_H
    2. #define CUSTOMTABLEMODEL_H
    3.  
    4. #include <QObject>
    5. #include <QAbstractTableModel>
    6.  
    7. #include <QDebug>
    8. #include <book.h>
    9.  
    10. class CustomTableModel : public QAbstractTableModel
    11. {
    12. Q_OBJECT
    13.  
    14. enum HeaderLabelsId {
    15. Title,
    16. Authors,
    17. ReleaseDate,
    18. About,
    19. Format,
    20. Image
    21. };
    22.  
    23. public:
    24. explicit CustomTableModel( QObject * parent = 0 );
    25. ~CustomTableModel();
    26.  
    27. int rowCount(const QModelIndex &parent) const override;
    28. int columnCount(const QModelIndex &parent) const override;
    29.  
    30. QVariant data(const QModelIndex& index, int role) const override;
    31. bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    32. QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    33. Qt::ItemFlags flags(const QModelIndex& index) const override;
    34.  
    35. void setHorizontalHeaderLabels(const QStringList &horizontalHeaderLabels);
    36. void insertRow( const Book & book );
    37. void removeRow(int row );
    38. void updateRow(int editingRow , const Book & book);
    39. const Book &row( int row );
    40.  
    41.  
    42. private:
    43. QList< Book > mBooks;
    44. const unsigned int mColumns = 6;
    45. QStringList mHorizontalHeaderLabels;
    46. };
    47.  
    48. #endif // CUSTOMTABLEMODEL_H
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    I tested this code with QStandardItemModel and it works fine, lessThan is called properly so I think that problem is in my CustomTableModel. Maybe I forgot about some important function which should be reimplemented?

  5. #5
    Join Date
    Jan 2014
    Posts
    76
    Thanks
    17
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    Very stupid mistake =-=. And you can see it in my code, exactly in mainwindow.h.
    First I tested sorting data with QSortFilterProxyModel and when I wrote my CustomSortFilterProxyModel I forgot to change the definitions of these classes in mainwindow.h....

    Sorry for that.

    But I have one more question - it is a good way for sort table by multiple column? or in Qt we have better way for this?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    You can always sort inside your model.
    I usually prefer that over using a proxy if the sorting gets more complicated.

    Cheers,
    _

  7. #7
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    But I have one more question - it is a good way for sort table by multiple column?
    QSortFilterProxyModel only supports sorting on a single column. You could probably write a lessThan() method that would do a multi-level sort for a custom QSortFilterProxyModel, but the proxy is still going to maintain its state under the assumption that it is a single-column sort. As anda_skoa says, it is usually easier to do this in the source model if the sorting is complex.

  8. #8
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    There is a reason for the focus on single-column sorting. If each sort is stable (which is usually expected in a UI), then you can get the lexicographic order on columns c1..cn by sorting by cn, then cn-1, ..., then c1. E.g. to sort files by date, then name, you can first click on the "name" column, then on the "date" column.

  9. #9
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QSortFilterProxyModel lessThan is never called.

    to sort files by date, then name, you can first click on the "name" column, then on the "date" column.
    I don't think that works in a custom model (or even in the off-the-shelf QSortFilterProxyModel) - unless the model (or proxy) implements such multi-column sort behavior. How does a standard QSortFilterProxyModel know to maintain the sort on one column that contains duplicates while sorting a second column with respect to those duplicates? As far as I have seen, sorting on any column occurs independently of other columns and results in all rows being reordered solely on the sort order of the chosen column.

Similar Threads

  1. QAbstractProxyModel lessThan [SOLVED]
    By killerwookie99 in forum Qt Programming
    Replies: 3
    Last Post: 15th September 2008, 20:42
  2. qSort with lessThan
    By soul_rebel in forum Qt Programming
    Replies: 4
    Last Post: 19th August 2008, 19:14
  3. QSortFilterProxyModel - lessThan sorts wrong column
    By ghorwin in forum Qt Programming
    Replies: 2
    Last Post: 21st July 2008, 10:06
  4. Replies: 6
    Last Post: 3rd November 2006, 12:53

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.