1
votes

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).

3
Apart from how this is done with Qt, can't you just draw the incremental changes into an image and then in the update just display this image?Christian Rau

3 Answers

2
votes

In addition to SingerOfTheFall's answer, you could also draw all your incremental changes into an image and then only draw this image in each update invocation.

For working with images Qt has a bunch of classes, most important being QImage and QPixmap and since they are both derived from QPaintDevice, they can be directly drawn into with a QPainter. Whereas QImage is optimized for direct pixel access and file I/O, QPixmap is optimized for showing it on the screen. It doesn't say which one is better for drawing into, but I would start with QPixmap and see how it performs.

There is also another "image" class you can draw into, QPicture. But this is not really an image storing the resulting rendering, but merely records the draw commands done with the QPainter to be easily played back later. Therefore I think it's performance shouldn't be much better than that of a "real" image. But it may be worth a try, especially if there is something more involved going on in the drawing and just storing the final image is not sufficient.

1
votes

QPainter simply can not save the "state", because it's not his purpose. The only thing it does is drawing. After you tell it to draw a line from [x,y] to [x1,y1], it draws it, and "forgets" everything. Each paintEvent() starts painting the widget from scratch. So, to add the elements you will have to redraw the existing ones each time.

0
votes

Actually I solve my problem using QPainterPath so 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);