Results 1 to 7 of 7

Thread: "QSortFilterProxyModel::lessThan" method doesn't work!

  1. #1
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question "QSortFilterProxyModel::lessThan" method doesn't work!

    Hello,

    I implement this virtual function from QSortFilterProxyModel so as to sort a table of datas.
    My proxy is between a QAbstractTableModel and a QTableView. My datas are strings.

    Here is the code :
    Qt Code:
    1. bool DSSortFilterProxy::lessThan(const QModelIndex &left,
    2. const QModelIndex &right) const
    3. {
    4. QVariant leftData = sourceModel()->data(left);
    5. QVariant rightData = sourceModel()->data(right);
    6. return QString::localeAwareCompare(leftData.toString(), rightData.toString()) < 0;
    7. }
    To copy to clipboard, switch view to plain text mode 

    The problem is during the runtime, the function is never called. (i tried to put a message pop-up inside...).

    How is this function called?
    Should it be called when the header of a column is clicked in the TableView?

    Thank you very much for your help.
    Xelag

  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: "QSortFilterProxyModel::lessThan" method doesn't work!

    Are you using Qt 4.1 or 4.2? If latter, you may use QTableView::setSortingEnabled() to disable row/column selection from headers and to enable sorting instead. As far as I remember, in Qt 4.1 you had to do the same by hand.

    From qtableview.cpp (Qt 4.2):
    Qt Code:
    1. void QTableView::setSortingEnabled(bool enable)
    2. {
    3. Q_D(QTableView);
    4. d->sortingEnabled = enable;
    5. horizontalHeader()->setSortIndicatorShown(enable);
    6. if (enable) {
    7. disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
    8. this, SLOT(selectColumn(int)));
    9. connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
    10. this, SLOT(sortByColumn(int)));
    11. sortByColumn(horizontalHeader()->sortIndicatorSection());
    12. } else {
    13. connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
    14. this, SLOT(selectColumn(int)));
    15. disconnect(horizontalHeader(), SIGNAL(sectionClicked(int)),
    16. this, SLOT(sortByColumn(int)));
    17. }
    18. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 2nd November 2006 at 19:21.
    J-P Nurmi

  3. #3
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: "QSortFilterProxyModel::lessThan" method doesn't work!

    Maybe you've already done this, but the header's sectionClicked( int ) signal needs to be connected to the TableView's sortByColumn( int ) slot... in 4.1 at least - not sure about 4.2.

  4. #4
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: "QSortFilterProxyModel::lessThan" method doesn't work!

    I use 4.1 Qt version.

    [...]in Qt 4.1 you had to do the same by hand.
    How can i do that?

    But i don't understand yet why the function lessThan is not called...

  5. #5
    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: "QSortFilterProxyModel::lessThan" method doesn't work!

    Quote Originally Posted by xelag View Post
    I use 4.1 Qt version.

    How can i do that?
    See my previous post, I added the piece of code from Qt 4.2.

    But i don't understand yet why the function lessThan is not called...
    QTableView is not sorting by default. Clicking on headers selects the whole row/column.
    J-P Nurmi

  6. #6
    Join Date
    Jan 2006
    Location
    Edmonton, Canada
    Posts
    101
    Thanks
    13
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: "QSortFilterProxyModel::lessThan" method doesn't work!

    Another thing is to ensure you've called setClickable( true ) on the header as well.

  7. #7
    Join Date
    Nov 2006
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Talking Re: "QSortFilterProxyModel::lessThan" method doesn't work!

    Thank you very much! it works!

    Code in the main programm :
    Qt Code:
    1. QHeaderView *tmpHeaderView = tableView->horizontalHeader();
    2. tmpHeaderView->setClickable(true);
    3.  
    4. tableView->setSortingEnabled(true);
    To copy to clipboard, switch view to plain text mode 

    Code in the class inherited form QTableView :

    Qt Code:
    1. void DSTableView::setSortingEnabled(bool _enable)
    2. {
    3.  
    4.  
    5. horizontalHeader()->setSortIndicatorShown(_enable);
    6.  
    7. if (_enable)
    8. {
    9. disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
    10. this, SLOT(selectColumn(int)));
    11.  
    12. connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
    13. this, SLOT(sortByColumn(int)));
    14.  
    15. sortByColumn(horizontalHeader()->sortIndicatorSection());
    16. }
    17. else
    18. {
    19.  
    20. connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
    21. this, SLOT(selectColumn(int)));
    22.  
    23. disconnect(horizontalHeader(), SIGNAL(sectionClicked(int)),
    24. this, SLOT(sortByColumn(int)));
    25. }
    26.  
    27. }
    To copy to clipboard, switch view to plain text mode 

    Thank you very much!
    Xelag

Similar Threads

  1. variable in method not initialized?!
    By frosch in forum Qt Programming
    Replies: 10
    Last Post: 3rd September 2006, 15:09
  2. Which version should I work on i686 host for arm target?
    By sliawati in forum Installation and Deployment
    Replies: 4
    Last Post: 11th August 2006, 10:44
  3. QScrollArea::scrollContentsBy does not work
    By Mad Max in forum Qt Programming
    Replies: 1
    Last Post: 19th April 2006, 15:48
  4. Static field and public method
    By probine in forum General Programming
    Replies: 1
    Last Post: 5th March 2006, 12:02
  5. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 13:02

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.