PDA

View Full Version : How to optimize the drawing process in paintEvent ?



yellowmat
26th April 2006, 10:12
Hello everybody,

As it is expressed in the thread title, I would like to know how to optimize the drawing process of a pixmap in the paintEvent function.

Let me give you some more explanations about my application. I have an interface between my application and a radio system so I am able to get the current radio frequency. I want to display the radio frequency in my application and I use ten pixmaps (one for each digits) which have some transparency. When a frequency scan is started, the digits are changed according to the real radio frequency and it flicks and I don't know how to do to eliminate it.

The widget which process the displaying of the digits is constructed with the following style :

Qt::WStyle_Customize|Qt::WStyle_NoBorder|Qt::WNoAu toErase

and here are the two functions I use to display the digit pixmaps :


void CMyClass::displayImage(const QPixmap& pix)
{
if( transparency )
{
if ( pix.mask() )
this->setMask( *pix.mask() );
}
// We can use the following code if we are sure that the source and destination graphic devices have the same depth
//QRect rect(event->rect());
//bitBlt(this, rect.topLeft(), &pixmap);

// We must use the following code if we are sure that the source and destination graphic devices have a different depth
QPainter painter(this);
painter.drawPixmap(0, 0, pix);
}

void CMyClass::paintEvent(QPaintEvent* event)
{
if( this->isShown )
{
if( loadingMode == CMyClass::Static )
{
QPixmap pixmap(images[imageCurrentIndex]);

displayImage(pixmap);
}
else if( loadingMode == CMyClass::Dynamic )
{
QPixmap pixmap(files[imageCurrentIndex]);

displayImage(pixmap);
}
}
}


So if someone could tell me how to optimize my diplaying process, it would be great.

Thanks in advance.

jpn
26th April 2006, 10:36
It could help a bit if you wouldn't construct a new pixmap in every paint event.
Thanks to implicit sharing though, the pixmaps share same data in CMyClass::Static mode.
The bigger problem might be in CMyClass::Dynamic mode. As if I understand correctly, it constructs a pixmap from a file in every paint event? That might cause some flickering..

Edit: How about if you would try to avoid reading pixmaps from files during the paint event?
In the "dynamic" mode you could draw the pixmaps from the images-array as well. You could make use of some timer or so and read the image files to the image-array once in a while to make it look like it loads the images on the fly. But don't read the pixmaps from files in the paint event.

yellowmat
26th April 2006, 10:43
Ok, that's true, it is not usefull to construct a Qpixmap object each time so I will correct this as soon as possible.

Thanks.