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. How Paint a background section in a QHeaderView?
QtWS25 Last Chance

How Paint a background section in a QHeaderView?

Scheduled Pinned Locked Moved Solved General and Desktop
qheaderview
2 Posts 2 Posters 363 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.
  • L Offline
    L Offline
    leonardo M B
    wrote on 4 Apr 2023, 15:44 last edited by leonardo M B 4 Apr 2023, 15:45
    #1

    So I was trying to color the background of certain cells based on the data on them, will implement the logic later. The problem Now Is that in the code below fillrect paints everything and erases the text of the header section. How Can I paint the text and the background color?

    void ResizableVerticalHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
    {
      painter->save();
      QHeaderView::paintSection(painter, rect, logicalIndex);
      painter->restore();
      painter->fillRect(rect, QBrush(QColor(0xE1, 0xFF, 0x3C)));
    }
    
    C 1 Reply Last reply 4 Apr 2023, 15:47
    0
    • L leonardo M B
      4 Apr 2023, 15:44

      So I was trying to color the background of certain cells based on the data on them, will implement the logic later. The problem Now Is that in the code below fillrect paints everything and erases the text of the header section. How Can I paint the text and the background color?

      void ResizableVerticalHeader::paintSection(QPainter* painter, const QRect& rect, int logicalIndex) const
      {
        painter->save();
        QHeaderView::paintSection(painter, rect, logicalIndex);
        painter->restore();
        painter->fillRect(rect, QBrush(QColor(0xE1, 0xFF, 0x3C)));
      }
      
      C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 4 Apr 2023, 15:47 last edited by Christian Ehrlicher 4 Apr 2023, 15:49
      #2

      Since you don't call the base class impl - what do you expect?

      Overrride initStyleOption() and set the desired color for QPalette::Window or even better override initStyleOptionForIndex()

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

      1 Reply Last reply
      1
      • L leonardo M B has marked this topic as solved on 6 Apr 2023, 16:40
      • R rudag referenced this topic on 8 Apr 2025, 21:17

      2/2

      4 Apr 2023, 15:47

      • Login

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