How do I remove previous paint and repaint in QGraphicsItem?
-
I have things drawn on the my QGraphicsItem object, and I have a wheelEvent that's going to draw new stuff on the item. When the wheelEvent happened, I wanted the old drawing to be removed and painted with the new stuff. I tried using update(), but it only adds the new drawing on top of the old one, it doesn't remove the old one.
void MyGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event) { if (event->delta() > 0) { setZoomFactor(zoomFactor + 1); } else { setZoomFactor(zoomFactor - 1); } setNewStuff(); update(); }
-
You could to clear your old background by your own -> e.g. with QPainter::fillRect()
-
You could to clear your old background by your own -> e.g. with QPainter::fillRect()
Can you elaborate more? I tried creating a clearPaint() inside the QGrpahicsItem class and called it before the update() in the wheelEvent but nothing changed.
void MyGraphicsItem::clearPaint() { QPainter *painter = new QPainter; QBrush blueBrush(Qt::blue); painter->fillRect(boundingRect(), blueBrush); } void MyGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event) { # ... setNewStuff(); clearPaint(); update(); }
-
Can you elaborate more? I tried creating a clearPaint() inside the QGrpahicsItem class and called it before the update() in the wheelEvent but nothing changed.
void MyGraphicsItem::clearPaint() { QPainter *painter = new QPainter; QBrush blueBrush(Qt::blue); painter->fillRect(boundingRect(), blueBrush); } void MyGraphicsItem::wheelEvent(QGraphicsSceneWheelEvent *event) { # ... setNewStuff(); clearPaint(); update(); }
@lansing said in How do I remove previous paint and repaint in QGraphicsItem?:
QPainter
QPainter should be used in paintEvent
-
You can only paint inside QGrpahicsItem::paint()
-
Okay I got it. Using fillRect() in the paint() worked.
void MyGraphicsItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { QBrush darkYellowBrush(Qt::darkYellow); QBrush redBrush(Qt::red); painter->fillRect(boundingRect(), darkYellowBrush); painter->drawline(...) }
Every time an is update() called from other functions, fillRect() will paint the empty rectangle first and then do all the drawing after.
Thanks for the help.
-
Then please mark the issue as solved, thx.