Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6569237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:36:05+00:00 2026-05-25T14:36:05+00:00

I am trying to change the background color for certain header sections. Some will

  • 0

I am trying to change the background color for certain header sections. Some will use the default coloring, others will get a different color.

The HeaderView doesn’t accept delegates like the QTreeView does; it does all the painting itself. It does this using two methods —

  • paintEvent
  • paintSection

My initial attempt was to try and override paintSection, letting it paint the default stuff, and then adding my own.

def paintSection(self, painter, rect, logicalindex):
    QHeaderView.paintSection(self, painter, rect, logicalindex)
    painter.save()
    painter.fillRect(rect, QBrush(Qt.red))
    painter.restore()

This doesn’t appear to do anything. It will not draw the filled rect. If I comment out the call to the base paintSection method, it will draw the filled rect, but not very consistently (i.e. clicking and resizing the header causes it to fill sometimes and not others).

Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T14:36:05+00:00Added an answer on May 25, 2026 at 2:36 pm

    There is no need to implement anythingQHeaderView can be changed through stylesheets like almost all widgets.

    Edit:

    You mentioned that you wanted to change the background color per column depending on data, the easiest way to do that is probably to derive a new model from QAbstractItemModel or another model class and reimplement the headerData() call

    QVariant QAbstractItemModel::headerData ( int section, Qt::Orientation orientation, int role = Qt::DisplayRole ) const [virtual]
    

    the role that you want to react to is Qt::BackgroundColorRole so the function could look like this

    QVariant VariableHeaderModel::headerData(int section Qt::Orientation orientation, int role)
    {
      QVariant result;
      if (role == Qt::BackgroundColorRole)
      {
        result = <QColor from custom processing>
      }
      else
      {
        result = Superclass::headerData(section, orientation, role);
      }
      return result;
    }
    

    Generally in Qt, the model decides what to show, almost all of the times change the model, not the view. Also the ‘data()’ calls get called a lot, I don’t know about the ‘headerData()’ but you probably want to cache any results if there is a lot of calculation going on.

    If you are using the QStandardItemModel you can probably just call

    setHeaderData(section, orientation, <aColor>, Qt::BackgroundColorRole);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change the background color of some rows in a ListBox
I'm trying to change background color of one of my UIView subclasses. For some
Using JQuery's jquery.animate-colors-min.js plugin I am trying to change the background color of some
I'm trying to change a div background color to a different color depending on
I'm new to JS. I'm trying to change button's background-color after .get() success. Here
How to change a background color of jQueryUI Progressbar? Im trying to use: $('#progressbar
I'm trying to change the background color of a single subplot in a MATLAB
I'm trying to change the background color of single cell be based on a
I am trying to change the Background Color of the ParentGrid when the child
I'm trying to change the background color of a program I did NOT write.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.