How can QPainter performance be improved?

2019-08-03 23:48发布

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.

2条回答
一夜七次
2楼-- · 2019-08-03 23:59

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.

查看更多
可以哭但决不认输i
3楼-- · 2019-08-04 00:06

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.

查看更多
登录 后发表回答