2
protected:
  virtual void paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
  {
    QHeaderView::paintSection(painter, rect, logicalIndex);
    painter->drawRect(2, 2, 10, 10);
  }

Rectangle is not painting. But when paintSection removed it is painting. I need to draw rectangle after call base paintSection.

Ufx
  • 2,595
  • 12
  • 44
  • 83

2 Answers2

0

As it was answered in this your question, rect is an area your should paint at.
If you paint outside of this area your drawings might be erased by painting of other cells.

So use rect to draw a rect:

painter->drawRect(rect.adjusted(2, 2, -2 , -2));
Community
  • 1
  • 1
Ezee
  • 4,214
  • 1
  • 14
  • 29
  • 1
    There seems to be one more problem - from what I saw, whenever you call QHeaderView::paintSection, all the other activities you do to the painter have no effect. E.g. I tried doing fillRect(rect), but it only works if QHeaderView::paintSection call is removed. The same if I draw a diagonal line across he cell. I wonder, what happens in QHeaderView::paint section that overrides everything else you do to the painter... – Serge Jun 15 '15 at 13:53
  • Are you doing your drawings after the call of QHeaderView::paintSection? – Ezee Jun 16 '15 at 12:39
  • I tried both before and after - updated code sample - neither works (I opened a separate question for that http://stackoverflow.com/questions/30847252/what-does-qheaderviewpaintsection-do-such-that-all-i-do-to-the-painter-before) – Serge Jun 16 '15 at 13:32
  • 1
    As @Serge notes, this answer does not solve the problem. I am hitting the same issue; calling super seems to make the painter non-functional. – bhaller Jul 30 '19 at 19:16
0

You need to protect the painter across the call to super, which modifies it. Try this:

painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore();

Also, as Ezee noted, you should be using the rect passed in as the basis for the coordinates you draw at; as suggested in that answer, something like:

painter->drawRect(rect.adjusted(2, 2, -2 , -2));
bhaller
  • 1,803
  • 15
  • 24