Question

I develop a browser-like application, where the canvas has large height and "ordinary" width, something like 1024x999999. I display a picture using 512 cached QPixmap blocks (1024x128), re-using them to display newly drawing areas. So if user scrolls around some given area of the large image, CPU is not busy, the cached blocks is used. So, this is how my engine works, briefly.

Want to implement a zoom. Don't know - smooth or discrete (x2, x3, x4...). Performance questions:

  • Is there any effective way to scale QPixmap on-the-fly in paintEvent() without allocating too much memory?
  • or maybe i should think about "zoom-layers" that cache zoomed picture for different zoom factors? But this makes smooth zooming impossible...
Was it helpful?

Solution

If you take a look at the documentation, you'll see that the paintEvent in fact receives a QPaintEvent object. This object has a getter method named region() that returns a QRect detailing the region to be repainted.

void QWidget::paintEvent ( QPaintEvent * event )
{
  QRect region = event->region();
  ...
}

So... you just need to repaint the part of the widget that is exactly inside that rectangle.

For your application, I recommend to calculate which image or images are within the rectangle, and redraw them accordingly, but only those images.

For the zoom part, Qt has optimized the way images are painted in QPainter objects if images are QPixmap objects. Or so they say...

So, you can write inside the paintEvent() method something like:

QPainter painter(this);
...
painter.drawPixmap(pos_x, pos_y, width, height, pixmap);
...

Hope that helped!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top