kostas petsis
kostas petsis

Reputation: 49

Qt- Moving a QGraphicsItem causes artifacts.Leaves trailes behind

When i move a QGraphicsItem, weird artifacts stay behind.Some parts of the item dont render,other render... In the note.cpp i have a shape

QPainterPath Note::shape()const{
   QPainterPath path;
   //    path.addRect(0, 0, 50, 20);
   path.moveTo(0, -80.0);
   path.lineTo(0.0, 80.0);
   path.lineTo(80.0, 0.0);
   //    path.lineTo(75.0, -30.0);
   path.closeSubpath();
   return path;
}

In the paint function

QPointF *points = new QPointF[3];
points[0] = QPointF(0,-80);
points[1] = QPointF(0,80);
points[2] = QPointF(80,0);
painter->drawPolygon(points,3);

enter image description here enter image description here The first picture shows that all are fine when i start the app. The second picture shows that when i move a triangle with the mouse, it gets sliced.Other times it leaves some trails and doesn't render all parts of the triangle Here is the github link for the project. Github link

To reproduce, just move a triangle.

Upvotes: 1

Views: 712

Answers (1)

eyllanesc
eyllanesc

Reputation: 243897

QGraphicsItem for efficiency only repaint the section that returns boundingRect() method, in your case QRect(0, 0, 80, 80) only returns half the necessary area since the coordinate (0, -80) is outside the boundingRect. The solution is:

QRectF Note::boundingRect() const {
    return QRectF(0, -80, 80, 160) ;
    // or
    // return shape().boundingRect();
}

Upvotes: 3

Related Questions