Qt随笔 - 窗口轻松实现阴影效果

网上看了许多实现阴影效果的方法都不如这个简单

效果图

(一)基本思路

先将所有窗口控件拖到一个QFrame,然后用setWindowFlags()设置窗口背景透明并使用QGraphicsDropShadowEffect让QFrame出现阴影,于是,窗口像是本身被有了阴影效果。

(二)具体实现

  • void QWidget::setAttribute(Qt::WidgetAttribute attribute, bool on = true)设置窗口背景透明
setAttribute(Qt::WA_TranslucentBackground);
  • QGraphicsDropShadowEffect子部件添加阴影
QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
shadow_effect->setOffset(-5, 5);
shadow_effect->setColor(Qt::gray);
shadow_effect->setBlurRadius(8);
ui->frame_main->setGraphicsEffect(shadow_effect);

注意要给QFrame上背景色

  • 完事

(三)参考

http://blog.sina.com.cn/s/blog_a6fb6cc90101eoop.html
http://www.cppblog.com/biao/archive/2009/06/12/87508.html

猜你喜欢

转载自blog.csdn.net/zx249388847/article/details/81283774