Results 1 to 5 of 5

Thread: how to change background color of individual QHeaderView section

  1. #1
    Join Date
    Dec 2013
    Location
    Chicago
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default how to change background color of individual QHeaderView section

    Hello,

    I have a project with a QTableView and a corresponding QAbstractTableModel. I have implemented a filter that can be applied to multiple columns. I need the column color (just the header section, not the entire column of the QTableView) to change if a filter is being applied to that column. I've made a couple of attempts and did not succeed. I'm starting to wonder if this is even possible.

    I tried setting the stylesheet, but it didn't do anything:

    Qt Code:
    1. QHeaderView::section:selected
    2. {
    3. background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
    4. stop:0 #00FF00, stop:1 #004C00);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I tried connecting the sectionClicked(int) signal in the QHeaderView to the model, to store the lastColumn clicked, and use this in headerData() to return a color if the role== Qt::BackgroundColorRole, but this did not work either.

    Qt Code:
    1. QVariant OrderTableModel::headerData(int section, Qt::Orientation orientation, int role) const
    2. {
    3. ...
    4. else if (role == Qt::BackgroundColorRole)
    5. {
    6. if (orientation == Qt::Horizontal) {
    7. if (section == m_nSectionClicked)
    8. {
    9. return QBrush(Qt::darkBlue);
    10. }
    11. else
    12. {
    13. return QBrush(Qt::black);
    14. }
    15. }
    16. }
    17. ...
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to change background color of individual QHeaderView section

    I tried setting the stylesheet, but it didn't do anything:

    Qt Code:
    1. QHeaderView::section:selected
    2. {
    3. background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1,
    4. stop:0 #00FF00, stop:1 #004C00);
    5. }
    To copy to clipboard, switch view to plain text mode 
    Use ":checked" instead of ":selected"
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Dec 2013
    Location
    Chicago
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change background color of individual QHeaderView section

    I tried, but nothing changed. Is a section supposed to be in a "checked" state if it is clicked? Eventually I'd like to have the column header background color change if a filter has been applied to it and alsof if that column is sorted, so I don't think this is possible through style sheets. After reading some other threads I decided to subclass the QHeaderView and override paintSection(). After doing this, I found that I couldn't get at the data because it is in the d-pointer. I couldn't subclass the QHeaderViewPrivate because it is not accessible. I just wanted to copy the code from
    Qt Code:
    1. QHeaderView::paintSection
    To copy to clipboard, switch view to plain text mode 
    into the overridden function and make the necessary modifications to change the background color. Am I going about this the wrong way?

    Thanks,
    Alex

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: how to change background color of individual QHeaderView section

    Ok I think you are facing a platform style problem. Refer this thread, it is discussed in there and a solution is also suggested.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Dec 2013
    Location
    Chicago
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to change background color of individual QHeaderView section

    Yes, thank you. I read that thread already and tried setting to plastique but it made everything look crappy. And it only works if I remove my style sheet entry for the QHeaderView::section. I have resorted to just putting brackets around the column name if the filter has been applied, which seems fine for now.

Similar Threads

  1. Change background color of QPushButton
    By gtthang in forum Qt Programming
    Replies: 3
    Last Post: 26th November 2013, 11:23
  2. QHeaderView header width and selected background color
    By TheRonin in forum Qt Programming
    Replies: 5
    Last Post: 24th October 2013, 10:18
  3. Replies: 1
    Last Post: 17th August 2010, 17:17
  4. Replies: 2
    Last Post: 17th July 2010, 22:07
  5. QHeaderView section resizes
    By dimuz in forum Qt Programming
    Replies: 7
    Last Post: 11th October 2007, 17:44

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.