Skip to content


Drawing to a Canvas in QT

I decided to rewrite the image handling in the Arthur fractal generator. So far I used a class derived from QLabel to show the generated images, but I faced some problems with this approach. QLabel is using QPixmap for showing an image. QPixmap is usually used for showing static images, but I want something more dynamic. E.g. showing the changes on the image when a new pixel or row is calculated in the fractal. I also need indexed images. So I decided to write a new widget using QImage for the image representation. QImage can handle indexed images. It also has another advantage. While QPixmap is stored on the X-server, QImage is stored locally. Therefore I can have direct access to its memory space, which makes rendering much faster.

I started googleing around to find some simple examples for showing a QImage and drawing pixels on it with immediate feedback. No luck. Finally I ended up with the cannonfield tutorial coming with qt sources. It has a widget, and a bullet is drawn on the widget while it is moving. It does not use QImage, but good enough for seeing the widget basics, updates etc. So I started to modify it. I removed all unnecessary stuff. I added a QImage as a private attribute. In the paint event handler method I draw the QImage.

I decided to make a moving dot bouncing from the image borders as a demo. I kept the timer from the original example. I connected the timer event to a slot which handles the dot moving and rewriting.
I also simplified the main.cpp. I have only this one widget to show. I got it working pretty fast and the results are convincing. Now, I go on to introduce the selection feature what I used in ArthurCanvas and I also try indexed image instead of the RGB image of the current example.

You can get the sources from here: qimage001

Posted in QT Programming.

Tagged with , , , .