Qt删除框架?

时间:2016-12-09 07:33:30

标签: c++ qt user-interface

在Qt中是否有一种有效的方法来移除框架并将背景颜色设置为透明?我想做的另一件事是让窗户也是“可移动的”。每当我按下并按住鼠标左键,我就可以在任何我喜欢的地方移动窗口。

以图形方式表达我想要达到的结果。

enter image description here

enter image description here

----------

解决方案,以使其“可移动”:https://forum.qt.io/topic/34354/solved-frameless-window-dragging-issue/2

1 个答案:

答案 0 :(得分:1)

首先你必须设置窗口标志(我在重载的QDialog :: exec中执行此操作):

setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);

(只需将Qt :: FramelessWindowHint添加到窗口标志中)

然后添加透明背景:

setAttribute(Qt::WA_TranslucentBackground);

...并确保未设置autoFillBackground(如果选中则取消选中Designer)

如果您需要添加阴影,只需将DropShadowEffect添加到创建背景圆角矩形的小部件:

auto dropShadow = new QGraphicsDropShadowEffect;
dropShadow->setOffset(0);
dropShadow->setBlurRadius(40);
dropShadow->setColor(QColor(0, 0, 0, 180));
ui.backgroundWidget->setGraphicsEffect(dropShadow);

阴影会在窗口小部件上绘制,因此您需要在背景窗口小部件周围留出额外空间。即如果你的BlurRadius设置为40,你应该设置一个40像素的边距:

layout()->setMargin(40);