Sorting QTreeView() columns by clicking on header
-
wrote on 8 Nov 2022, 11:11 last edited by
I've used
treeView->setSortingEnabled(true);
to sort the view by clicking the view's horizontal header and it doesn't work only for last column. When I click the last column there appears sort indicator (when I click its direction change up/down) but the column doesn't sort. The rest of the columns work good.
What can I do to fix it? I've not found such a problem anywhere. -
I've used
treeView->setSortingEnabled(true);
to sort the view by clicking the view's horizontal header and it doesn't work only for last column. When I click the last column there appears sort indicator (when I click its direction change up/down) but the column doesn't sort. The rest of the columns work good.
What can I do to fix it? I've not found such a problem anywhere. -
wrote on 21 Nov 2022, 09:34 last edited by
In last column I hold QMetaType. I created enum class MyType where I assign numbers to value and declare AMetaType MyType. I added also
inline bool operator<(MyType left, MyType right) { return static_cast<std::underlying_type_t<MyType>>(left) < static_cast<std::underlying_type_t<MyType>>(right); }
for use in context where the enum needs to be sortable but sorting still doesn't work.
-
In last column I hold QMetaType. I created enum class MyType where I assign numbers to value and declare AMetaType MyType. I added also
inline bool operator<(MyType left, MyType right) { return static_cast<std::underlying_type_t<MyType>>(left) < static_cast<std::underlying_type_t<MyType>>(right); }
for use in context where the enum needs to be sortable but sorting still doesn't work.
-
Lifetime Qt Championwrote on 21 Nov 2022, 15:51 last edited by Christian Ehrlicher
And how do you use this type?
-
@Mary02
Maybe this was worth mentioning in the first place, since this is the only column you say does not sort? Did you put aqDebug()
statement into your method to verify it is getting called? -
wrote on 22 Nov 2022, 07:56 last edited by JonB
@Mary02
That is why I would put aqDebug()
in! So now you know why your method has no effect. As @Christian-Ehrlicher asked, you now have to show/explain how/where you use your type and why you think that method should be called during sorting. Qt treats the data in model columns/rows asQVariant
s, why would it even be interested in/now about your enum type during its sorting? -
@Mary02
That is why I would put aqDebug()
in! So now you know why your method has no effect. As @Christian-Ehrlicher asked, you now have to show/explain how/where you use your type and why you think that method should be called during sorting. Qt treats the data in model columns/rows asQVariant
s, why would it even be interested in/now about your enum type during its sorting?wrote on 22 Nov 2022, 08:48 last edited by@JonB
Ok, I am brand new in Qt/C++. As you mentioned Qt treats data in model as QVariant, I used qDebug() to print the data from the last column, converting it to QVariant, and it turned out that instead of my values, there are empty fields. It's strange because another column that also contains my own variable types displays the correct values after conversion. -
@JonB
Ok, I am brand new in Qt/C++. As you mentioned Qt treats data in model as QVariant, I used qDebug() to print the data from the last column, converting it to QVariant, and it turned out that instead of my values, there are empty fields. It's strange because another column that also contains my own variable types displays the correct values after conversion.wrote on 22 Nov 2022, 09:31 last edited by JonB@Mary02 said in Sorting QTreeView() columns by clicking on header:
also contains my own variable types displays the correct values after conversion
And does that call an
inline bool operator<(MyType left, MyType right)
method you have written for that type? Or does not, and just happens to output in anticipated order so you would not know?I am brand new in Qt/C++
Then what made you write that less-than method for your type, and what makes you think anything will call it?
And btw: I would assume that for an enumerated type left to its own devices Qt will treat an enumerated type value as an
int
and use that for any comparison purposes. Is that not sufficient/working for your case? -
Again: how do you use this enum? Do you have a QStandardItemModel, a custom model derived from QAbstractItemModel or do you use a QTreeWidget instead a QTreeView?
-
Again: how do you use this enum? Do you have a QStandardItemModel, a custom model derived from QAbstractItemModel or do you use a QTreeWidget instead a QTreeView?
wrote on 23 Nov 2022, 07:58 last edited by@Christian-Ehrlicher
I use a QTreeView. -
@Christian-Ehrlicher
I use a QTreeView.