Sort QTreeView on multiple columns
-
wrote on 24 May 2023, 20:59 last edited by
Hi,
I Have a tree view which contains data in table format like:
Col1 Col2 Col3 Col4 ...
H1
B C D
E F G
H2
H I J
K L MI want to sort tree data based on multiple columns. Lets say on col1 and col2.
I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
I have tried sorting like:
m_TreeView->sortByColumn(1, Qt::AscendingOrder);
m_TreeView->sortByColumn(0, Qt::AscendingOrder);
so that col2 will be sorted followed by col1 but it didn't worked.
Can Anyone please suggest right approach to implement this? -
Hi,
I Have a tree view which contains data in table format like:
Col1 Col2 Col3 Col4 ...
H1
B C D
E F G
H2
H I J
K L MI want to sort tree data based on multiple columns. Lets say on col1 and col2.
I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
I have tried sorting like:
m_TreeView->sortByColumn(1, Qt::AscendingOrder);
m_TreeView->sortByColumn(0, Qt::AscendingOrder);
so that col2 will be sorted followed by col1 but it didn't worked.
Can Anyone please suggest right approach to implement this?wrote on 24 May 2023, 22:03 last edited by@AntriX said in Sort QTreeView on multiple columns:
Col1 Col2 Col3 Col4 ...
H1
B C D
E F G
H2
H I J
K L MCol1 Col2 Col3 Col4 H1 B C D E F G H2 H I J K L M
Tree structure looks like one shown above.
-
Hi,
I Have a tree view which contains data in table format like:
Col1 Col2 Col3 Col4 ...
H1
B C D
E F G
H2
H I J
K L MI want to sort tree data based on multiple columns. Lets say on col1 and col2.
I am using QSortFilterProxyModel to sort. I have implemented my own lessThan to sort as per my requirements.
I have tried sorting like:
m_TreeView->sortByColumn(1, Qt::AscendingOrder);
m_TreeView->sortByColumn(0, Qt::AscendingOrder);
so that col2 will be sorted followed by col1 but it didn't worked.
Can Anyone please suggest right approach to implement this?wrote on 24 May 2023, 22:03 last edited by@AntriX said in Sort QTreeView on multiple columns:
but it didn't worked.
What happened?
sortByColumn()
will only sort by one column. You cannot call it multiple times to produce sort by multiple columns. You have to implement that logic yourself (via yourlessThan()
taking into account secondary columns). -
@AntriX said in Sort QTreeView on multiple columns:
but it didn't worked.
What happened?
sortByColumn()
will only sort by one column. You cannot call it multiple times to produce sort by multiple columns. You have to implement that logic yourself (via yourlessThan()
taking into account secondary columns).wrote on 25 May 2023, 11:17 last edited by -
wrote on 25 May 2023, 11:24 last edited by
@AntriX
You need to design your own way for the user to tell you that they want such-and-such column as the secondary sort column. Then in yourlessThan()
if 2 values compare equal on the primary column look up the values in the secondary column and compare those to determine what it should return when the primary columns are equal. -
@AntriX
You need to design your own way for the user to tell you that they want such-and-such column as the secondary sort column. Then in yourlessThan()
if 2 values compare equal on the primary column look up the values in the secondary column and compare those to determine what it should return when the primary columns are equal. -
6/6