"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 :
Code:
bool DSSortFilterProxy
::lessThan(const QModelIndex &left,
{
QVariant leftData
= sourceModel
()->data
(left
);
QVariant rightData
= sourceModel
()->data
(right
);
return QString::localeAwareCompare(leftData.
toString(), rightData.
toString()) <
0;
}
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
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):
Code:
{
d->sortingEnabled = enable;
horizontalHeader()->setSortIndicatorShown(enable);
if (enable) {
disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sortByColumn(int)));
sortByColumn(horizontalHeader()->sortIndicatorSection());
} else {
connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
disconnect(horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sortByColumn(int)));
}
}
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.
Re: "QSortFilterProxyModel::lessThan" method doesn't work!
I use 4.1 Qt version.
Quote:
[...]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...
Re: "QSortFilterProxyModel::lessThan" method doesn't work!
Quote:
Originally Posted by
xelag
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.
Quote:
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.
Re: "QSortFilterProxyModel::lessThan" method doesn't work!
Another thing is to ensure you've called setClickable( true ) on the header as well.
Re: "QSortFilterProxyModel::lessThan" method doesn't work!
Thank you very much! it works!
Code in the main programm :
Code:
QHeaderView *tmpHeaderView
= tableView
->horizontalHeader
();
tmpHeaderView->setClickable(true);
tableView->setSortingEnabled(true);
Code in the class inherited form QTableView :
Code:
void DSTableView::setSortingEnabled(bool _enable)
{
horizontalHeader()->setSortIndicatorShown(_enable);
if (_enable)
{
disconnect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
connect(horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sortByColumn(int)));
sortByColumn(horizontalHeader()->sortIndicatorSection());
}
else
{
connect(horizontalHeader(), SIGNAL(sectionPressed(int)),
this, SLOT(selectColumn(int)));
disconnect(horizontalHeader(), SIGNAL(sectionClicked(int)),
this, SLOT(sortByColumn(int)));
}
}
Thank you very much!
Xelag