Search code examples
c++linuxqtqwidgetqpainter

QPainter Save State


I have a QWidget in which I use a QPainter object to draw some dots but when update() method is invoked, the QWidget's draw is cleared completely. Is there any way to save the actual state and just add dots, or i have to save every dot and paint them in every paintEvent() call ? Basically when I press an arrow, I must show a line on the QWidget (it's for a car rally).


Solution

  • I solved my problem using QPainterPath so that I can group ellipses to draw 'dynamic' lines:

    QPainterPath* p = new QPainterPath(this); //this should be a class attribute to save all points
    p->addEllipse(myCustomPoint); //we should add the points dynamically
    
    QPainter painter(this); // On QPainter::paintEvent;
    painter.drawPath(p);