PDA

View Full Version : PaintEvent Performance



meazza
23rd June 2011, 12:24
Hello fellow programmers.

I have a question regarding my paintEvent in my class which is derived from QWidget. And what I am asking here is why does this paintEvent take up 70-80 % of my cpu when I add this object to a QGraphicsScene and move it around.

I know I can get a significant boost in performance by setting the QGraphcsItem::DeviceCoordinateCache flag as cacheMode but I am just curios why my paintEvent is so heavy on the CPU and if I can somehow improve it.

PS. The pixmap that I am using is only loading the first time I create the object since I store it in the QPixmapCache and use it from there.


void CDITachometer::paintEvent(QPaintEvent *)
{

/* Min rotate(-93)
* Max rotate(162)
* Mid rotate(36)
*/
QPainter boolPainter(this);
boolPainter.setRenderHint(QPainter::SmoothPixmapTr ansform);

QPixmap pixmap = *QPixmapCache::find(accessibleName());

boolPainter.drawPixmap(0, 0, pixmap.scaled(size().width(), size().height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));

QTransform transform2;
transform2.translate(size().width()/2, size().height()/2);
transform2.rotate(m_rotation);
transform2.translate(-size().width()/2, -size().height()/2);

boolPainter.setTransform(transform2);

pixmap = *QPixmapCache::find(accessibleName().append("1"));

boolPainter.drawPixmap(0, 0, pixmap.scaled(size().width(), size().height(),
Qt::IgnoreAspectRatio, Qt::SmoothTransformation));
}

Thanks in advance and I appritiate all advice and comments

wysota
23rd June 2011, 21:09
It takes lot of time because you are scaling two pixmaps that are probably quite big. You'll get better performance if you prescale them and then insert them into the pixmap cache so that the paint event only extracts the pixmaps and renders them.

And think if your item really needs to be a QWidget subclass. As I understand it is, currently.

meazza
27th June 2011, 09:49
It takes lot of time because you are scaling two pixmaps that are probably quite big. You'll get better performance if you prescale them and then insert them into the pixmap cache so that the paint event only extracts the pixmaps and renders them.
Yeah the pixmaps are 500 x 500 which I guess is pretty big. The reason they are scaled is because you can change the size of the widget and the pixmaps are always scaled to the widget size. I did remove the scaling from paintevent now because I dont need to do it if size has not changed, which gave me a bit more performance.


And think if your item really needs to be a QWidget subclass. As I understand it is, currently.
Yes it is a subclass of QWidget at the moment. Would I get better performance if I used QGraphicsItem or QGraphicsWidget?

wysota
27th June 2011, 10:04
Yes, you would get better performance if the item was a graphics item and not a widget.