如何在 Qt 中为无框窗口添加阴影 [英] How to put a shadow to a Frameless window in Qt
问题描述
我正在尝试创建一个下面有阴影的无边框窗口.当我们创建一个带有 Qt::FramelessWindowHint 标志的无边框窗口时,它不包含阴影.但是我们可以通过创建一个 QGraphicsDropShadowEffect 对象,然后通过 setGraphicsEffect 函数将其传递给小部件,从而轻松地为子部件添加阴影.但这似乎不适用于 QMainWindow.请帮我在 Qt 中为无框窗口添加阴影...
I'm trying to create a Frameless window that has a shadow under it.When we create a borderless window with Qt::FramelessWindowHint flag it doesn't contain a shadow. But we can put shadows to a child widgets easy by creating a QGraphicsDropShadowEffect object and then pass it to the widget through setGraphicsEffect function. But this doesn't seem to work for QMainWindow. Please help me to put shadow to a frameless window in Qt...
推荐答案
你可以使用这个简单的 hack 来做到:
You can do it using this simple hack:
将QWidget"(比如小部件)添加到 MainWindow 并将 MainWindow 上的所有内容移动到小部件.然后这样做:
Add a "QWidget" (say widget) to the MainWindow and move everything that's on the MainWindow to the widget. Then do this:
setAttribute(Qt::WA_TranslucentBackground); //enable MainWindow to be transparent
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect();
effect->setBlurRadius(5);
ui->widget->setGraphicsEffect(effect);
这似乎对我有用.见:
This seems to work for me. See:
这篇关于如何在 Qt 中为无框窗口添加阴影的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!