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 8778793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:36:59+00:00 2026-06-13T19:36:59+00:00

My custom table model derives from QAbstractTableModel and is then displayed in a QTableView

  • 0

My custom table model derives from QAbstractTableModel and is then displayed in a QTableView.

Looks like this:

enter image description here

I would like to change the text color for certain row headers, which can be decided in the model. Is it possible to color certain headers from there? I couldn’t find a way so far. What I found was about setting background/text color for all headers, not for special few. The color is supposed to be kind of markup for the user.

  • 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-06-13T19:37:01+00:00Added an answer on June 13, 2026 at 7:37 pm

    What you have to do is reimplement QAbstractTableModel::headerData().
    Depending on the value of section (header index starting at zero) you can individually style the header items.
    The relevant values for foreground (=text color) and background in Qt::ItemDataRole are Qt::BackgroundRole and Qt::ForegrondRole

    E.g. like this:

    QVariant MyTableModel::headerData(int section, Qt::Orientation orientation, int role) const {
      //make all odd horizontal header items black background with white text
      //for even ones just keep the default background and make text red
      if (orientation == Qt::Horizontal) {
        if (role == Qt::ForegroundRole) {
           if (section % 2 == 0)
              return Qt::red;
           else
             return Qt::white;
        }
        else if (role == Qt::BackgroundRole) {
           if (section % 2 == 0)
              return QVariant();
           else
             return Qt::black;
        }
        else if (...) {
          ...
          // handle other roles e.g. Qt::DisplayRole
          ...
        }
        else {
          //nothing special -> use default values
          return QVariant();
        }
      }
      else if (orientation == Qt::Vertical) {
          ...
          // handle the vertical header items
          ...
      }
      return QVariant();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using custom permissions in my Django models like this: class T21Turma(models.Model): class
Im using a using a QTableView in conjuction with table model derived from QAbstaractTableModel
I have data from a database loaded into a JTable through a custom table
I'm defining my custom model for pas.plugin.sqlalchemy. Existing table of login details have binary
I have a custom mysql table we'll call module_identifiers and a corresponding model in
I've a subclass of JTable that uses a custom table model (an implementation of
I have generated the data table model using the option Generate model from database
I have implemented a custom Table Model as follows: public class MyTableModel extends AbstractTableModel
I've got a QTableView with a custom model, and it works fine. However, when
Shown below is the my custom table model. I am trying to use that

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.