PDA

View Full Version : "QSortFilterProxyModel::lessThan" method doesn't work!



xelag
2nd November 2006, 18:01
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 :

bool DSSortFilterProxy::lessThan(const QModelIndex &left,
const QModelIndex &right) const
{
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

jpn
2nd November 2006, 18:10
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):


void QTableView::setSortingEnabled(bool enable)
{
Q_D(QTableView);
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)));
}
}

Jimmy2775
2nd November 2006, 18:20
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.

xelag
2nd November 2006, 18:21
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...

jpn
2nd November 2006, 18:24
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.

Jimmy2775
2nd November 2006, 18:46
Another thing is to ensure you've called setClickable( true ) on the header as well.

xelag
3rd November 2006, 11:53
Thank you very much! it works!

Code in the main programm :

QHeaderView *tmpHeaderView = tableView->horizontalHeader();
tmpHeaderView->setClickable(true);

tableView->setSortingEnabled(true);

Code in the class inherited form QTableView :


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