Qt doesn't provide classes for widgets with not rectangular shape. If the design of your GUI needs more than standard forms of Qt, you have to paint the shape of the graphic elements, place the picture on a Qt widget and place transparency bitmask upon the widget.
Unfortunatelly Qt (3.2) doesn't provide an easy and efficient method for creating transparency mask for a pixmap. There is only QImage::createHeuristicMask(). Which is rather slow. Besides, it creates the transparency only in surrounding but not in the inner areas.
I changed a little bit this method. You can specify, which color should be transparent,
or by default the transparent color will be taken from the position 0x0 (left:upper pixel).
For easy usage of non rectangular widgets I wrote 3 klasses ShapeWidget, ShapeButton and ShapeForm. The code shows a constructor of my
sample mp3-player application.
MP3::MP3( QWidget *p, const QString &iname ): ShapeForm( p, iname),
_bplay(this, "img/mp3_butplay.png"),
_bforw(this, "img/mp3_butfw.png"),
_bbackw(this, "img/mp3_butbw.png"),
_bclose(this, "img/mp3_butclose.png")
{
//set geometry of all members
}
that's all! The buttons _bplay, _bforw,... are created with the picture name as
parameter. This pictures shape the form of the buttons.
Create a ShapeWidget with an image. Usefull, if Images are embedded in code (e.g as xpm)
ShapeWidget(QWidget *, const QImage &, long=-1);
ShapeWidget(QWidget *, const QString &, long=-1);
void setShape( const QImage &, long =-1); void setShape( const QString &, long =-1);
void setWidgetShape( QWidget *, const QString &, long =-1); static void setWidgetShape( QWidget *, const QImage &, long =-1);
void setPressedShape( const QImage &, long =-1); void setPressedShape( const QString &, long =-1);
The original project was: http://www.heinitz-it.de/projects/fancy_but/index.html
As Alexander Atamas from codeskipper.com offered me to write an article for codeskipper.com about it, I made the code user friendlier through ShapeWidget - "library".
There is a very similar article "A Skinnable Qt Application" on codeskipper.com.