PDA

View Full Version : Faster paintEvent



SailinShoes
27th September 2008, 18:02
The paintevent that sets background bitmap and draws the needle takes ~220 ms to execute on my laptop.

How I can I optimize the paintevent and make it faster to execute?




QPixmap Meter::setBitmap()
{

QPixmap* pixmap;

if(mReset)
{

mReset = false;
pixmap = & pixmap1;

}
else
{

if(mStatus == true)
{

pixmap = & pixmap2;

}
else if(mStatus == false )
{

if( mFinalTorque <= mTorque)
{

pixmap = & pixmap3;

}
else
{

pixmap = & pixmap4;

}

}

}

return *pixmap;

}

void Meter::paintEvent(QPaintEvent* pe)
{
struct timeval tv1,tv2;

gettimeofday(&tv1, NULL);

myPaintEvent(pe);

gettimeofday(&tv2, NULL);

qDebug() << "Meter::paintEvent: " << (tv2.tv_sec-tv1.tv_sec)*1000000+(tv2.tv_usec-tv1.tv_usec) << "us";
}

void Meter::myPaintEvent(QPaintEvent*)
{

QPainter paintNeedle(this);
QColor needleColor(255, 230, 0);

paintNeedle.setBackgroundMode (Qt::TransparentMode);
paintNeedle.drawPixmap(mPixmapXCoord, mPixmapYCoord, setBitmap());

paintNeedle.translate(mVectorTranslationX, mVectorTranslationY);

paintNeedle.setPen(Qt::NoPen);
paintNeedle.setBrush(needleColor);

paintNeedle.save();
paintNeedle.rotate(mFinalPoint);
paintNeedle.drawConvexPolygon(needle, 3);
paintNeedle.restore();

paintNeedle.setPen(needleColor);

QRect rect(mEllipseX, mEllipseY, mEllipseWidht, mEllipseHeight);
paintNeedle.drawEllipse(rect);

}

SailinShoes
1st October 2008, 16:25
Of course the paintEvent does not take 220ms? My mistake, on my embedded arm the paintevent takes ~7ms.

Its a kind of speedometer that is painted...The background makes up of four bitmaps because the meter can be in four different states.



paintNeedle.drawPixmap(mPixmapXCoord, mPixmapYCoord, setBitmap());


...takes ~5ms and the rendering of the needle below takes about ~2ms.

I wonder if it is more effective to render the background states instead?