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:
Code:
{
// Do the strings look like they are BPM counts?
const QString bpmPattern
= "^[0-9]+\\.[0-9]+$";
if (sourceModel
()->data
( left
).
toString().
trimmed().
indexOf(QRegExp(bpmPattern
)) == 0 && sourceModel
()->data
( right
).
toString().
trimmed().
indexOf(QRegExp(bpmPattern
)) == 0) { double leftBPM = sourceModel()->data( left ).toString().trimmed().toDouble();
double rightBPM = sourceModel()->data( right ).toString().trimmed().toDouble();
qDebug() << "BPM Comparasion leftBPM =" << leftBPM << "rightBPM =" << rightBPM;
if (leftBPM == 0.0) rightBPM = -1 * rightBPM;
if (rightBPM == 0.0) leftBPM = -1 * leftBPM;
return leftBPM < rightBPM;
}
....
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)
Code:
{
const Qt::SortOrder sortOrder = ((MixxxView *)this->parent())->m_pTrackTableView->horizontalHeader()->sortIndicatorOrder();
// Do the strings look like they are BPM counts?
const QString bpmPattern
= "^[0-9]+\\.[0-9]+$";
if (sourceModel
()->data
( left
).
toString().
trimmed().
indexOf(QRegExp(bpmPattern
)) == 0 && sourceModel
()->data
( right
).
toString().
trimmed().
indexOf(QRegExp(bpmPattern
)) == 0) { double leftBPM = sourceModel()->data( left ).toString().trimmed().toDouble();
double rightBPM = sourceModel()->data( right ).toString().trimmed().toDouble();
if (leftBPM == 0.0 && sortOrder == Qt::AscendingOrder) rightBPM = -1 * rightBPM;
if (rightBPM == 0.0 && sortOrder == Qt::AscendingOrder) leftBPM = -1 * leftBPM;
// qDebug() << "BPM Comparasion leftBPM =" << leftBPM << "rightBPM =" << rightBPM;
return leftBPM < rightBPM;
}
....
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.