1
votes

I have elipses on QWidget that are drawn using QPainter, is there a way to delete a certain ellipse (at a certain coordinate)?

Thanks

1
use QGraphicsItem, that will make is more easierVinod Paul
Using QGraphics framework will cause rewrite of at least that one widget and possibly much more. To the question: change paint method logic and schedule redraw of a widget.divanov

1 Answers

0
votes

Don't paint it the next time.

On each paint event, an implementation of paintEvent() draws "from scratch", either the whole widget, or the rect/region specified in QPaintEvent. So each paintEvent starts on an empty canvas, not on the contents from the previous paint. Thus, if you want to erase something, you must trigger a repaint via update(), and then just omit the elements you don't want to have painted.

void Speedometer::setSpeedLabelEnabled( bool enabled ) {
     if ( m_speedLabelEnabled == enabled )
         return;
     m_speedLabelEnabled = enabled;
     update(); // trigger repaint
}

void Speedometer::paintEvent( QPaintEvent* ) {
     QPainter p( this );
     if ( m_speedLabelEnabled ) {
         p.drawEllipse( ... );
         p.drawText( ..., m_currentSpeed, ... );
     }
}

As Vinod Paul says: QGraphicsView might be a good option, in case you have to manage many such elements.