QTableView : display indicator on column header to display which ones can be sorted
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!
Re: QTableView : display indicator on column header to display which ones can be sort
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.
Re: QTableView : display indicator on column header to display which ones can be sort
Thank you d_stranz for your answer, it helps much!
Quote:
Originally Posted by
d_stranz
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.
Quote:
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
Quote:
"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."
Re: QTableView : display indicator on column header to display which ones can be sort
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.