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:
{
// 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;
}
....
bool SortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
{
// 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;
}
....
To copy to clipboard, switch view to plain text mode
Bookmarks