Implement paintSection for QHeaderView delivered class

十年热恋 提交于 2020-01-05 00:48:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!