Right way to sort by checkble column in treeview using QSortFilterProxyModel subclass
-
i have treeview subclass that using QSortFilterProxyModel subclass to sort by column items , when the item is string it is working fine
but when the item is checkble type , and i like to sort by checked items . in the QSortFilterProxyModel it just dosnt work.
here is my code for lessThan methods and where i capture the checkbox state.@void SettingsDialog::tree_itemChanged(QStandardItem *item)
{Qt::CheckState checkState = item->checkState(); if(checkState == Qt::Checked) {
item->setCheckState(Qt::Checked);
item->setData(true,Qt::UserRole);} else if(checkState == Qt::Unchecked) {
item->setCheckState(Qt::Unchecked);
item->setData(false,Qt::UserRole);}
}
bool SettingsSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
{int columnLeftIndx = left.column();
int columnRightIndx = right.column();if(columnLeftIndx == SETTINGS_CHECKBOX_CELL_IDX)
{bool leftUint = sourceModel()->data( left,Qt::UserRole ).toBool();
bool rightUint = sourceModel()->data( right,Qt::UserRole ).toBool();
return leftUint < rightUint;
}
@what im doing wrong here ?
-
Is this code complete? It looks like part of the lessThan method is missing?
-
it just check if the sort coming from the second column and then try to do the sort .
it is complete , as i dont really know what else i need to put -
The code seems incomplete, because the last brace is not there. So I was wondering if something else might have been dropped.
Perhaps you should be more clear on what kind of result you are expecting. I guess you want your checked items to be sorted above the unchecked ones, but you still want to keep sorting on the text itself as well, right?
Edit:
So, is this what you are after?
Derrek
Zach
Andy
Bernard
Freddy
William -
Hi
the last brace just missing when i copied the text , i just want the checked item to be first.
with not text related to the sort .
the check-boxs are in separated item.
Thanks again -
Try this:
@
bool SettingsSortFilterProxyModel::lessThan( const QModelIndex &left, const QModelIndex &right ) const
{
int columnLeftIndx = left.column();
int columnRightIndx = right.column();ASSERT(columnLeftIndx == columnRightIndx);
if(columnLeftIndx == SETTINGS_CHECKBOX_CELL_IDX)
{bool l = sourceModel()->data( left,Qt::UserRole ).toBool();
bool r = sourceModel()->data( right,Qt::UserRole ).toBool();
return (l && !r); //only return true if the left item is checked and the right one is not--
} else {
return QSortFilterProxyModel::qLess(left, right);
}
}
@ -
Hi thanks for the replay , but it dosn't work . i must doing something wrong .
@bool l = sourceModel()->data( left,Qt::UserRole ).toBool();
bool r = sourceModel()->data( right,Qt::UserRole ).toBool();@
always gives me true. no matter what .
also :
@QSortFilterProxyModel::qLess(left, right);
gives me : error C2039: 'qLess' : is not a member of 'QSortFilterProxyModel'@ -
So why not use the checkStateRole directly then?
Note that the function name should be lessThan(), not qLess()
-
Finally ! Thanks Andre