如何在 Qt 的小部件下绘制阴影? [英] How would I draw a shadow under a widget in Qt?

查看:52
本文介绍了如何在 Qt 的小部件下绘制阴影?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何在 Qt 中的小部件(这不是主要小部件,例如标签)下绘制阴影.我需要使用样式表还是编写它(用 C++)?

解决方案

假设您有一个表单和一个标签,要从中投射阴影.

您可以像这样使用 类,QGraphicsDropShadowEffect 也派生自该类.

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++)?

解决方案

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:

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.

这篇关于如何在 Qt 的小部件下绘制阴影?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆