How can QPainter performance be improved?

2019-08-03 23:45发布

问题:

I have a large display (about 1000x2000 pixels) and I'm doing the following to draw images to the screen:

QImage *pImage = GetImage(); // from wherever
QPainter painter(this);   
painter.drawImage((0,0), *pImage);  // this line takes over 100ms to complete.

The larger the screen is that I'm drawing to, the longer this paint takes. I guess the pImage is being memcpy'd and that's the difference. How can I reduce this overhead? I'm not trying to scale or anything here.

Thanks.

回答1:

You're painting a QImage. Don't do that, try with a QPixmap instead.

From the QImage documentation:

QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen."

Depending on the platform, getting the QImage data into the format and location needed for painting, can be extremely expensive.

P.S.: There is no need to create QImages on the heap, as

QImage objects can be passed around by value since the QImage class uses implicit data sharing.



回答2:

One simple improvement you can make is to draw only the area that needs updated (if you can). The QPaintEvent contains a rect for the changed area, and the QPainter::drawImage has overloads that can take rects for the portion to draw.

You might also look at the ImageConversionFlags options for faster options.