PDA

View Full Version : Improve performance QPainter



Programm3r
11th November 2009, 16:18
Hi All!

This code create a shadow image with only a rectangle where the image is viewable.
but is slow! :(

Any idea!
thank lot !



void QOverView::drawBitmap(QPainter* pPainter)
{

// Create shadow bitmap
QPixmap aShadowPixmap(m_rcRatio.width(),m_rcRatio.height() );
aShadowPixmap.fill(VSRECT_COLOR_SHADOW);

QPainter painterMask(&aShadowPixmap);
painterMask.setPen(QPen(QBrush(VSRECT_COLOR_BORDER ),2));
painterMask.drawRect(m_rcVisualRect);

QPen aPen(VSRECT_COLOR_TRANSPARENT);
QBrush aBrush(VSRECT_COLOR_TRANSPARENT);
painterMask.setPen(aPen);
painterMask.setBrush(aBrush);
painterMask.fillRect(m_rcVisualRect.adjusted(2,2,-2,-2), aBrush);
painterMask.end();

QBitmap aMsk = aShadowPixmap.createMaskFromColor(VSRECT_COLOR_TRA NSPARENT);
aShadowPixmap.setMask(aMsk);

// Shadow operation
QPixmap pxmResult = QPixmap(m_rcRatio.size());
QPainter painterResult(&pxmResult);
painterResult.setCompositionMode(QPainter::Composi tionMode_Source);
painterResult.fillRect(pxmResult.rect(), Qt::transparent);
painterResult.setCompositionMode(QPainter::Composi tionMode_SourceOver);
painterResult.drawPixmap(0,0, aShadowPixmap);
painterResult.setCompositionMode(QPainter::Composi tionMode_DestinationOver);
painterResult.drawPixmap(m_rcRatio, m_aPixmap);
painterResult.end();

// Blitting image
pPainter->drawPixmap(m_nOffsetCenterX,m_nOffsetCenterY,pxmRe sult);
}

wysota
11th November 2009, 16:45
You shouldn't do such complex things in the paint event. Move everything apart the last line to a separate function that is called whenever the visual state of your widget changes to update a pixmap that you will blit to the widget with the call from the last line.

Programm3r
12th November 2009, 08:23
Thank you WySota!