问题
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.
回答1:
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));
回答2:
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));
来源:https://stackoverflow.com/questions/25489640/implement-paintsection-for-qheaderview-delivered-class