有没有一种有效的方法可以在Qt中去掉框架并将背景色设置为透明?另外我想做的一件事是使窗口也能够“移动”,即每当我按住左键时,我可以将窗口移动到任何我喜欢的位置。
图形上表达我想要实现的结果。
图形上表达我想要实现的结果。
----------
为使其“可移动”的解决方案:https://forum.qt.io/topic/34354/solved-frameless-window-dragging-issue/2
为使其“可移动”的解决方案:https://forum.qt.io/topic/34354/solved-frameless-window-dragging-issue/2
首先你需要设置窗口标志(我在重载的QDialog::exec函数中进行此操作):
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
只需将Qt::FramelessWindowHint添加到您的窗口标志中即可。
然后您可以添加透明背景:
setAttribute(Qt::WA_TranslucentBackground);
确保autoFillBackground
未设置(如果已经选中,请在设计器中取消选中)。
如果需要添加阴影,只需将DropShadowEffect添加到创建背景圆角矩形的小部件中即可:
auto dropShadow = new QGraphicsDropShadowEffect;
dropShadow->setOffset(0);
dropShadow->setBlurRadius(40);
dropShadow->setColor(QColor(0, 0, 0, 180));
ui.backgroundWidget->setGraphicsEffect(dropShadow);
layout()->setMargin(40);