Sections

How can I draw shadows behind text?

 

Answer:


You can achieve this by painting the shadow yourself. One way of doing this is to draw the text at an offset position (dx, dy) using black at a low opacity. This will make a hard-edge shadow. To make the shadow appear more blurry one can draw it multiple times at locations around the offset position at an even lower opacity .e.g

painter.setOpacity(0.5);
drawText(dx, dy, text);
painter.setOpacity(0.2);
drawText(dx+1, dy, text);
drawText(dx-1, dy, text);
drawText(dx, dy+1, text);
drawText(dx, dy-1, text);

We do this in the deform demo, see:

http://doc.trolltech.com/4.3/demos-deform.html

Also, see the source code in QTDIR/demos/shared/arthurstyle

and the drawComplexControl() function which uses this approach.

Another approach is to draw the text into a QImage and then run a blur algorithm on the image. Then you can draw the image at a small offset and draw the text on top of it.

back