PDA

View Full Version : QTableView : display indicator on column header to display which ones can be sorted



olap74
18th March 2020, 15:25
Hi all,

I have a QTableView with many columns. Few of them allow a sort, not the other ones. In order to help the user to identify which columns can be sorted, I would like to add a sort indicator, for example like a double arrow, both ascending and descending QT sort indicator arrow) . But I can't find a way to do this, and also I think only one column can have a sort indicator at the same time, right ? Do you have an idea to address the problem ?
Thank you for your help!

d_stranz
18th March 2020, 16:48
QHeaderView has a showSortIndicator property. Does this only show the indicator on the currently sorted column?

You could also try implementing a Qt::DecorationRole in your model's headerData() method to set an icon for those columns that can be sorted. Something like the sort toolbar buttons that are shown in Excel or other spreadsheet apps.

olap74
20th March 2020, 09:30
Thank you d_stranz for your answer, it helps much!


QHeaderView has a showSortIndicator property. Does this only show the indicator on the currently sorted column?
yes, the showSortIndicator property is not enough, the Indicator is shown just for the current sorted column.



You could also try implementing a Qt::DecorationRole in your model's headerData() method to set an icon for those columns that can be sorted. Something like the sort toolbar buttons that are shown in Excel or other spreadsheet apps.
I succeeded to use Qt::DecorationRole in my model's headerData() method to set an icon for those columns that can be sorted. The challenge now is to position it wherever I want, and eventually discard the QT icon indicator and use one icon more consistent with the one I used to indicate which columns could be sorted.
But I'm in doubt when I read the end of the details of the QHeaderView documentation https://doc.qt.io/qt-5/qheaderview.html#details

"Note: Each header renders the data for each section itself, and does not rely on a delegate. As a result, calling a header's setItemDelegate() function will have no effect."

d_stranz
20th March 2020, 18:11
You can try implementing the Qt::AlignmentRole for the headerData() method, but this only permits you to left / right / center. You could possibly derive your own class from QHeaderView and replace the virtual QHeaderView::paintSection() method for the columns where you want a customized appearance, and call the base class implementation otherwise. You would then replace the standard vertical or horizontal (I forget which one is for the columns) QHeaderView instance with an instance of your derived class.