Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I draw a shadow under a widget in Qt?

I am wondering how I would draw a shadow under a widget (which isn't the main widget, say, a label.) in Qt. Would I neeed use a stylesheet or would I code it (in C++)?

like image 709
Alex Spataru Avatar asked Sep 08 '12 05:09

Alex Spataru


1 Answers

Say you have a form and a label you want to cast a shadow from.

You can use QGraphicsDropShadowEffect like so:

QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setBlurRadius(5);
effect->setXOffset(5);
effect->setYOffset(5);
effect->setColor(Qt::black);

label->setGraphicsEffect(effect);

And the effect would be:

enter image description here

The downside of this effect is that if you apply it to a widget, all its children will inherit it. This could be problematic if you apply the effect on a widget with a lot of widgets because it could slow down the rendering time. But for your example this is perfectly fine and recommended.

For more information about effects in Qt check the QGraphicsEffect class from which QGraphicsDropShadowEffect is also derived.

like image 157
Iuliu Avatar answered Sep 30 '22 14:09

Iuliu