Results 1 to 4 of 4

Thread: QTableView : display indicator on column header to display which ones can be sorted

  1. #1
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Sep 2016
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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 View Post
    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:ecorationRole 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."
    Last edited by olap74; 20th March 2020 at 11:00.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 1
    Last Post: 20th November 2015, 10:12
  2. Get data from QTableView sorted by column
    By qks1 in forum Qt Programming
    Replies: 0
    Last Post: 26th June 2013, 10:27
  3. QtTreeWidget: issue with indicator display
    By gjaegy in forum Qt Programming
    Replies: 12
    Last Post: 30th May 2012, 16:18
  4. Changing QTableView column display order
    By scarleton in forum Qt Programming
    Replies: 3
    Last Post: 18th October 2010, 01:04
  5. Currently sorted column in QTableView
    By borges in forum Newbie
    Replies: 1
    Last Post: 21st September 2007, 17:52

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.