QGraphicsItem绘制延迟

时间:2017-03-06 08:15:55

标签: python pyside qgraphicsview qgraphicsitem qpainter

enter image description here

可能的原因是什么?当我放大QGraphicsView并移动QGraphicsItem时,我得到了这个奇怪的结果。如果我再次缩放或平移视图或者我专注于其他小部件,它会更新。我正在使用PySide。画家的功能是这个

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)

为什么即使是这个基本的油漆事件也会发生这种情况?如果没有笔,则不会出现此问题。如果我增加笔的宽度,这个问题是令人不安的。

3 个答案:

答案 0 :(得分:0)

我不知道这个渲染工件的实际解决方案。但是,在mouseMoveEvent期间更新视图确实解决了这个问题。

 def mouseMoveEvent(self, event):
    QGraphicsView.mouseMoveEvent(self, event)
    if self.scene().selectedItems():
        self.update()

答案 1 :(得分:0)

您看到的错误可能是因为所绘制的部分不在边界矩形内。我的猜测是您使用与计算边界矩形相同的值来计算所绘制的矩形。然后应用一支笔会使绘制的矩形超出边界,因此会导致您看到的污迹。

答案 2 :(得分:0)

我遇到了同样的问题。这是我的解决方案:

正如@Nathan Mooth 所说,问题是我在 boundingRect 之外绘制,所以我只绘制了圆角矩形(我在 paint() 方法中绘制的)10 个单位widthheight 小于 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)

注意:我添加了颜色选择并更改了阴影的颜色。所以看起来有点不同。