[Qt-interest] fastest way to draw everchanging pixels

Reto Glauser qt at blinkeye.ch
Sat Mar 21 14:16:33 CET 2009


Hello

I'm porting a project from Java (Swing) to QT. I try to visualize a gas
simulation and have to draw atoms and walls on the screen. I use 1 pixel per
object, e.g. a blue pixel is an atom and a red pixel is a wall. Since this is a
simulation I'm looking for the fastest way to draw everchanging pixels.

At the moment I'm extending QWidget and reimplement the QWidget::paintEvent()
(following the "Basic Drawing Example"):

> void RenderArea::paintEvent(QPaintEvent *)
> {
> 	QPainter painter(this);
> 	painter.scale( scale, scale );
> 
> 	for( int i=0; i<height; i++ )
> 		for( int j=0; j<width; j++ )
> 			painter.drawPoint( ... );
> }

But this does not scale. While this works for a 100x100 drawing area (~75ms per
event) scaling +1 (e.g. drawing a 200x200 area) drops to ~200 ms per event.
Note: The above example does not reveal that most of the drawing area is empty
and I only draw pixels when necessary (e.g. if an atom or wall is there).

With Java I was able to visualize a drawing area of 1024x1024 in ~200 ms with
what I think is the same logic (e.g. painting every pixel):

> public void paintComponent(Graphics g) {
>     ...
>     if (state == State.ATOM)
>     {
>         // Set current drawing color
>         g.setColor(new Color( ...));
>         g.fillRect( ... );
>     }
>     else if (state == State.WALL)
>     {
>         // Set current drawing color
>         g.setColor(new Color(198, 48, 52));
>         g.fillRect( ... );
>     }
> }

I obviously need another mechanism to visualize the simulation in QT. At the
moment hardware accelerated support (e.g. OpenGL) is not an option (which is not
used by Java either). Any hints?

r,
r



More information about the Qt-interest-old mailing list