【问题标题】:Qt removing the frame?Qt删除框架?
【发布时间】:2016-12-09 07:33:30
【问题描述】:

有没有一种有效的方法可以在 Qt 中移除框架并将背景颜色设置为透明?我想做的另一件事是让窗口也可以“移动”,例如每当我按住鼠标左键时,我都可以随意移动窗口。

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

---------

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

【问题讨论】:

    标签: c++ qt user-interface


    【解决方案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);
    

    阴影是在小部件本身上绘制的,因此您需要在背景小部件周围留出额外的空间。 IE。如果您的 BlurRadius 设置为 40,您应该设置 40 像素的边距:

    layout()->setMargin(40);
    

    【讨论】:

    • 哇,太棒了!如上所述如何使其“可移动”有什么想法吗?
    • 不知道有没有简单的方法让它“可移动”。我会覆盖mouseDownEvent、mouseUpEvent 和mouseMoveEvent。使用 mouseDownEvent 存储相对于小部件和父小部件的鼠标位置,并在 mouseMoveEvent 中设置小部件位置,以便存储的小部件相对位置最终低于当前父小部件相对鼠标位置。
    • 我认为这是一个值得讨论的问题。
    • 我想出了如何让它可以移动,有兴趣的可以看看这里:forum.qt.io/topic/34354/solved-frameless-window-dragging-issue/…
    猜你喜欢
    • 1970-01-01
    • 2021-12-18
    • 2014-04-30
    • 2016-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多