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 1.4k Views 1 Watching
  • 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.
  • rudagR Offline
    rudagR Offline
    rudag
    wrote on 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?

    Christian EhrlicherC jsulmJ jeremy_kJ 3 Replies Last reply
    0
    • rudagR rudag

      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?

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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

      rudagR 1 Reply Last reply
      0
      • rudagR rudag

        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?

        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • Christian EhrlicherC Christian Ehrlicher

          @rudag said in QHeaderView background color for each section:

          bg.value<QBrush>()

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

          rudagR Offline
          rudagR Offline
          rudag
          wrote on 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);
          
          Christian EhrlicherC 1 Reply Last reply
          0
          • rudagR rudag

            @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);
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 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

            rudagR 1 Reply Last reply
            0
            • rudagR rudag

              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?

              jeremy_kJ Offline
              jeremy_kJ Offline
              jeremy_k
              wrote on 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
              • Christian EhrlicherC Christian Ehrlicher

                @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.

                rudagR Offline
                rudagR Offline
                rudag
                wrote on last edited by rudag
                #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);
                
                jeremy_kJ 1 Reply Last reply
                0
                • rudagR rudag

                  @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);
                  
                  jeremy_kJ Offline
                  jeremy_kJ Offline
                  jeremy_k
                  wrote on 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

                  • Login

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