Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget
Forum Update on Monday, May 27th 2025

Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 4.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tester
    wrote on last edited by VRonin
    #1

    The setBackground() function CANNOT set the item's background brush to the specified brush on the HorizontalHeaderItem of QTableWidget.

    setForeground() function is OK.

    Why?

    Do you have any advices ? Thanks.

    #include <QApplication>
    
    #include <QTableWidget>
    #include <QTableWidgetItem>
    
    #include <QHeaderView>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QTableWidget *table = new QTableWidget;
    
        table->setColumnCount(4);
        table->setRowCount(2);
    
    
        QStringList header;
        header<<"1"<<"2"<<"3"<<"4";
        table->setHorizontalHeaderLabels(header);
    
        for(int i=0; i<4; i++)
        {
    
            QString strtmp;
    
            QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i);
            columnHeaderItem->setBackground(QBrush(QColor(0,255,0)));///////no effect,why???
            columnHeaderItem->setForeground(QBrush(QColor(0,0,255)));///////OK!
            columnHeaderItem->setText(strtmp.setNum(i));
    
            table->setHorizontalHeaderItem(i,columnHeaderItem);
    
    
            QTableWidgetItem *columnHeaderItem1 = new QTableWidgetItem;
            columnHeaderItem1->setBackground(QBrush(QColor(0,255,0)));/////OK!
            columnHeaderItem1->setForeground(QBrush(QColor(255,0,0)));
            columnHeaderItem1->setText(strtmp.setNum(i+4));
    
            table->setItem(0,i,columnHeaderItem1);
    
    
            QTableWidgetItem *columnHeaderItem2 = new QTableWidgetItem;
            columnHeaderItem2->setBackground(QBrush(QColor(255,255,0)));///////////////OK!
            columnHeaderItem2->setForeground(QBrush(QColor(0,0,255)));
            columnHeaderItem2->setText(strtmp.setNum(i+8));
    
            table->setItem(1,i,columnHeaderItem2);
    
        }
    
        table->show();
    
        return a.exec();
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      try replacing

      QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i);
       columnHeaderItem->setBackground(QBrush(QColor(0,255,0)));
      

      with

      QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i);
      Q_ASSERT(columnHeaderItem);
       columnHeaderItem->setData(Qt::BackgroundRole,QBrush(QColor(0,255,0)));
      

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      mrjjM 1 Reply Last reply
      0
      • VRoninV VRonin

        try replacing

        QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i);
         columnHeaderItem->setBackground(QBrush(QColor(0,255,0)));
        

        with

        QTableWidgetItem *columnHeaderItem = table->horizontalHeaderItem(i);
        Q_ASSERT(columnHeaderItem);
         columnHeaderItem->setData(Qt::BackgroundRole,QBrush(QColor(0,255,0)));
        
        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @VRonin said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:

        Q_ASSERT(columnHeaderItem);

        Good idea, but It is non null.
        The columnHeaderItem->setBackground(QBrush(QColor(0, 255, 0)));
        does seems to have no effect :)
        When used in header.

        alt text

        1 Reply Last reply
        1
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          In this case you probably want to set the stylesheet of the headerview or reimplement its paintEvent

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          mrjjM 1 Reply Last reply
          1
          • VRoninV VRonin

            In this case you probably want to set the stylesheet of the headerview or reimplement its paintEvent

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @VRonin
            Guess so.
            But is it not strange the items can change background color when used in the
            data cells but not in the cells in HeaderView ?
            I guess the Qt::BackgroundColorRole is used differently.

            VRoninV 1 Reply Last reply
            0
            • mrjjM mrjj

              @VRonin
              Guess so.
              But is it not strange the items can change background color when used in the
              data cells but not in the cells in HeaderView ?
              I guess the Qt::BackgroundColorRole is used differently.

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #6

              @mrjj said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:

              But is it not strange the items can change background color when used in the data cells but not in the cells in HeaderView ?

              It is strange indeed, I checked the sources and the background is definitely used in QHeaderView::paintSection

              QVariant backgroundBrush = d->model->headerData(logicalIndex, d->orientation,
                                                                  Qt::BackgroundRole);
                  if (backgroundBrush.canConvert<QBrush>()) {
                      opt.palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
                      opt.palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
                      painter->setBrushOrigin(opt.rect.topLeft());
                  }
              

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              mrjjM 1 Reply Last reply
              1
              • VRoninV VRonin

                @mrjj said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:

                But is it not strange the items can change background color when used in the data cells but not in the cells in HeaderView ?

                It is strange indeed, I checked the sources and the background is definitely used in QHeaderView::paintSection

                QVariant backgroundBrush = d->model->headerData(logicalIndex, d->orientation,
                                                                    Qt::BackgroundRole);
                    if (backgroundBrush.canConvert<QBrush>()) {
                        opt.palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
                        opt.palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
                        painter->setBrushOrigin(opt.rect.topLeft());
                    }
                
                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @VRonin
                Hmm Good info. It should work then.
                Thank you for the code check. ! :)

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @VRonin @mrjj, unless I'm mistaken, the style might be ultimately ignoring it.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  VRoninV 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    @VRonin @mrjj, unless I'm mistaken, the style might be ultimately ignoring it.

                    VRoninV Offline
                    VRoninV Offline
                    VRonin
                    wrote on last edited by
                    #9

                    @SGaist said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:

                    unless I'm mistaken

                    You never are

                    from QWindowsStyle::drawControl

                    case CE_HeaderSection: {
                            QBrush fill;
                            if (opt->state & State_On)
                                fill = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
                            else
                                fill = opt->palette.brush(QPalette::Button);
                    

                    so, at least in windows, it's ignored

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    mrjjM 1 Reply Last reply
                    2
                    • VRoninV VRonin

                      @SGaist said in Cannot set the backgroud color of the HorizontalHeaderItem of QTableWidget:

                      unless I'm mistaken

                      You never are

                      from QWindowsStyle::drawControl

                      case CE_HeaderSection: {
                              QBrush fill;
                              if (opt->state & State_On)
                                  fill = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
                              else
                                  fill = opt->palette.brush(QPalette::Button);
                      

                      so, at least in windows, it's ignored

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @VRonin
                      Super.
                      So even Header accepts an QTableWidgetItem,
                      the setBackground Brush will not be used.
                      Thank you for digging :)

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved