Title: QGraphicsItem paint delay
Tags: python;pyside;qgraphicsview;qgraphicsitem;qpainter
Question: What could be the possible reason for this? When i zoom in the QGraphicsView and move the QGraphicsItem, I get this weird result. It does update if I zoom or pan the View again or if I focus on other widgets. Im using PySide. And the painter function is this
def paint(self, painter, option, widget):
if self.isSelected():
brush = self.highlight_brush
pen = self.highlight_pen
else:
brush = self.dormant_brush
pen = self.dormant_pen
painter.setBrush(brush)
painter.setPen(pen)
painter.drawRect(0, 0, 100, 100)
Why does this happen even for this basic paint event? This problem is not seen if there is no Pen. If I increase the pen width, this issue is disturbingly visible.


Here is another answer: The error you are seeing is probably because parts of what you are drawing are outside the bounding rectangle. My guess is you are using the same values to calculate the rectangle you are drawing as you are to calculate the bounding rectangle. Applying a pen then will make the drawn rectangle wider than the bounds and so will result in the smearing you are seeing.

Here is another answer: I don't know the actual solution for this rendering artifacts. But, updating the view during mouseMoveEvent did fix the issue.
def mouseMoveEvent(self, event):
QGraphicsView.mouseMoveEvent(self, event)
if self.scene().selectedItems():
self.update()
Comment for this answer: I looked into this a bit yesterday, I suspected it was due to the view needing to be notified somehow that it needed to update, but couldn't see what the 'correct' method was. Presumably when the items have update() called they signal the view they belong to to redraw?
Comment for this answer: Infact, the mouseMoveEvent i posted above belongs to the View. Since the artifacts happen only when there is something selected, updating the view during mouse movement(when there is an active selection) does solve the problem. But, I still dont understand why this happens. As i mentioned above, there is absolutely no problem when there is no pen

Here is another answer: I had the same problem. This is my solution:
As @Nathan Mooth said, the problem was that I was drawing outside of the boundingRect, so I just made my rounded rectangle(what I'm drawing in the paint() method) 10 units width and height less than the boundingRect:
# Setup Rect
frameRect = self.boundingRect()
frameRect.setWidth(self.boundingRect().width() - 10)
frameRect.setHeight(self.boundingRect().height() - 10)
This is how it was looking before(GIF):
This is how it looks now(GIF)
Note: I added color selection and changed the color of the drop shadow. So it looks a bit different.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:03/06/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><pyside><qgraphicsview><qgraphicsitem><qpainter>', 'Title': 'QGraphicsItem paint delay', 'ViewCount': 387, 'id': 42620601, 'user_AboutMe': '', 'user_AccountId': 9532493.0, 'user_DisplayName': 'vaishak', 'user_Reputation': 63.0, 'user_Views': 6.0, 'user_id': 7083648.0}}