Home · Examples 

Pixmap Filters Example

The Pixmap Filters example uses the QPixmapFilters that come with Qt to process pixmaps.

The abstract QPixmapFilter class provides an interface for implementing filters for QPixmaps. Qt comes with three ready-made subclasses: QPixmapConvolutionFilter, QPixmapColorizeFilter, and QPixmapDropShadowFilter.

The example lets the user select a filter from the Pixmap Filter combo box. The left copy of the pixmap will remain unchanged, while the right will be applied to the selected filter. The effect of the filter can be customized by handling the widgets below the pixmaps. More on this later.

The example consists of two classes: FilterExample and FilterWidget. We will now go through the code concerning pixmap filters; it lives in the FilterWidget class.

FilterWidget Class Definition

The FilterWidget class sets up the selected filter and draws the pixmaps on itself each time it receives a paint event. The event is triggered when any of the example's widgets change their value.
Missing snippet: examples/painting/pixmapfilters/filterexample.h.

We will apply sourcePixmap to the filters. Since we need room for the shadow of
QPixmapDropShadowFilter, we draw the source on a background pixmap (the grid of squares as seen in the image above).

The GUI is set up using Qt Designer (check out its manual if you need to beef up your designer skills). The designer form has three parent widgets, which contain the widgets necessary to customize each filter respectively.

FilterWidget Class Implementation

The constructor simply assigns the private variables discussed in the previous section, so we dive right into the painting code.
Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

The setupFilter() function sets up the selected filter based on the values of the widgets; we'll examine it shortly. The filter then draws the processed pixmap over the backgroundPixmap.

Note that resultSize is a constant value, and that the positions of the pixmaps are fixed.

Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

First, we make sure to hide the widget that customizes the previous filter (of course, chances are that it is the same as the one we will use now). We then set up the selected filter. Let's look at the code for one filter at a time.
Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

The convolution kernel is fetched from the line edits, and then set on the convolution filter. The kernel can be changed with the line edits, which are connected to the
update() slot of our FilterWidget.

Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

With the colorize filter, we control the shades of red, green, and blue that we want the pixmap to have.

Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

The drop shadow filter lets us change the position, color, and the edge-blur of the shadow.
Missing snippet: examples/painting/pixmapfilters/filterexample.cpp.

Finally, we return the filter.

Copyright © 2009 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt Jambi 4.5.2_01