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. QHeaderView background color for each section

QHeaderView background color for each section

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 218 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.
  • R Offline
    R Offline
    rudag
    wrote on 8 Apr 2025, 21:17 last edited by
    #1

    I have the same problem as in this topic:
    https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderview

    I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)

    My last attempt was:

    void HeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
    {
        QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
        painter->save();
        QHeaderView::paintSection(painter, rect, logicalIndex);
        painter->restore();
        if(bg.isValid())
            painter->fillRect(rect, bg.value<QBrush>());
    }
    

    Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?

    C J J 3 Replies Last reply 9 Apr 2025, 05:14
    0
    • R rudag
      8 Apr 2025, 21:17

      I have the same problem as in this topic:
      https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderview

      I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)

      My last attempt was:

      void HeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
      {
          QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
          painter->save();
          QHeaderView::paintSection(painter, rect, logicalIndex);
          painter->restore();
          if(bg.isValid())
              painter->fillRect(rect, bg.value<QBrush>());
      }
      

      Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 9 Apr 2025, 05:14 last edited by
      #2

      @rudag said in QHeaderView background color for each section:

      bg.value<QBrush>()

      Do you really return a QBrush in headerData() for Qt::BackgroundRole?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply 9 Apr 2025, 20:06
      0
      • R rudag
        8 Apr 2025, 21:17

        I have the same problem as in this topic:
        https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderview

        I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)

        My last attempt was:

        void HeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
        {
            QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
            painter->save();
            QHeaderView::paintSection(painter, rect, logicalIndex);
            painter->restore();
            if(bg.isValid())
                painter->fillRect(rect, bg.value<QBrush>());
        }
        

        Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?

        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 9 Apr 2025, 05:14 last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • C Christian Ehrlicher
          9 Apr 2025, 05:14

          @rudag said in QHeaderView background color for each section:

          bg.value<QBrush>()

          Do you really return a QBrush in headerData() for Qt::BackgroundRole?

          R Offline
          R Offline
          rudag
          wrote on 9 Apr 2025, 20:06 last edited by
          #4

          @Christian-Ehrlicher Yes, like below:

          modelM->clear();
          modelM->setColumnCount(ops.count());
          modelM->setRowCount(funcs.count());
          modelM->setHorizontalHeaderLabels(ops);
          modelM->setVerticalHeaderLabels(funcs);
          //setting only one headerData to test
          modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
          
          C 1 Reply Last reply 10 Apr 2025, 04:21
          0
          • R rudag
            9 Apr 2025, 20:06

            @Christian-Ehrlicher Yes, like below:

            modelM->clear();
            modelM->setColumnCount(ops.count());
            modelM->setRowCount(funcs.count());
            modelM->setHorizontalHeaderLabels(ops);
            modelM->setVerticalHeaderLabels(funcs);
            //setting only one headerData to test
            modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);
            
            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 10 Apr 2025, 04:21 last edited by
            #5

            @rudag said in QHeaderView background color for each section:

            modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);

            Here you only call setData()... but i asked what data() returns.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            R 1 Reply Last reply 11 Apr 2025, 19:04
            0
            • R rudag
              8 Apr 2025, 21:17

              I have the same problem as in this topic:
              https://forum.qt.io/topic/144127/how-paint-a-background-section-in-a-qheaderview

              I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)

              My last attempt was:

              void HeaderView::paintSection(QPainter * painter, const QRect & rect, int logicalIndex) const
              {
                  QVariant bg = model()->headerData(logicalIndex, Qt::Horizontal, Qt::BackgroundRole);
                  painter->save();
                  QHeaderView::paintSection(painter, rect, logicalIndex);
                  painter->restore();
                  if(bg.isValid())
                      painter->fillRect(rect, bg.value<QBrush>());
              }
              

              Also I dont know how to override initStyleOptionForIndex() as @Christian-Ehrlicher mentioned in the topic above. Any help?

              J Offline
              J Offline
              jeremy_k
              wrote on 10 Apr 2025, 17:45 last edited by
              #6

              @rudag said in QHeaderView background color for each section:

              I have a QTableView with dynamic number of headers and would like to have them in different colors (logic will come later, just need to paint right now)

              My last attempt was:

              You didn't say what is unsatisfactory about this attempt.

              Is there another reason to implement a custom header view? QHeaderView already supports a background color.

              #include <QApplication>
              #include <QTableView>
              #include <QStandardItemModel>
              #include <QColor>
              
              int main(int argc, char *argv[])
              {
                  QApplication a(argc, argv);
              
                  QStandardItemModel model;
                  QStandardItem h1("test");
                  model.appendRow(&h1);
              
                  model.setHeaderData(0, Qt::Orientation::Horizontal, QColorConstants::Green, Qt::ItemDataRole::BackgroundRole);
              
                  QTableView view;
                  view.setModel(&model);
                  view.show();
                  return a.exec();
              }
              

              2ca0a9fb-66f1-4a10-bad2-d72b0c40f86a-image.png

              Asking a question about code? http://eel.is/iso-c++/testcase/

              1 Reply Last reply
              2
              • C Christian Ehrlicher
                10 Apr 2025, 04:21

                @rudag said in QHeaderView background color for each section:

                modelM->setHeaderData(0, Qt::Horizontal, QBrush(Qt::blue), Qt::BackgroundRole);

                Here you only call setData()... but i asked what data() returns.

                R Offline
                R Offline
                rudag
                wrote on 11 Apr 2025, 19:04 last edited by rudag 4 Nov 2025, 19:07
                #7

                @Christian-Ehrlicher said in QHeaderView background color for each section:

                Here you only call setData()... but i asked what data() returns.

                It's working now. I didn't know I had to call QHeaderView's show() explicitly.

                painter->drawText(rect, Qt::AlignCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString());

                But it's lost the mouse hover effect, bold text when column is selected, etc.. do I really have to write all these effects?

                @jeremy_k said in QHeaderView background color for each section:

                You didn't say what is unsatisfactory about this attempt.

                Is there another reason to implement a custom header view? QHeaderView already supports a background color.

                Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.

                QStandardItem *i = new QStandardItem("Test");
                i->setBackground(QBrush(Qt::yellow));
                modelM->setHorizontalHeaderItem(0, i);
                
                J 1 Reply Last reply 11 Apr 2025, 20:48
                0
                • R rudag
                  11 Apr 2025, 19:04

                  @Christian-Ehrlicher said in QHeaderView background color for each section:

                  Here you only call setData()... but i asked what data() returns.

                  It's working now. I didn't know I had to call QHeaderView's show() explicitly.

                  painter->drawText(rect, Qt::AlignCenter, model()->headerData(logicalIndex, Qt::Horizontal, Qt::DisplayRole).toString());

                  But it's lost the mouse hover effect, bold text when column is selected, etc.. do I really have to write all these effects?

                  @jeremy_k said in QHeaderView background color for each section:

                  You didn't say what is unsatisfactory about this attempt.

                  Is there another reason to implement a custom header view? QHeaderView already supports a background color.

                  Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.

                  QStandardItem *i = new QStandardItem("Test");
                  i->setBackground(QBrush(Qt::yellow));
                  modelM->setHorizontalHeaderItem(0, i);
                  
                  J Offline
                  J Offline
                  jeremy_k
                  wrote on 11 Apr 2025, 20:48 last edited by
                  #8

                  @rudag said in QHeaderView background color for each section:

                  Tried your setHeaderData() and didn't work. I really would like a solution without subclass. I tried like bellow and background is okay, but it doesn't show the text.

                  Did you try the full and unmodified program? That suggests that you've found a bug in Qt. If it was instead adapted into a non-working program, that doesn't tell me anything meaningful. The snippets of the nonworking program posted so far aren't enough to work with.

                  Asking a question about code? http://eel.is/iso-c++/testcase/

                  1 Reply Last reply
                  1

                  6/8

                  10 Apr 2025, 17:45

                  • Login

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