QFrame圆形边框透明背景 [英] QFrame round border transparent background

查看:100
本文介绍了QFrame圆形边框透明背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 QFrame 创建一个圆形上下文菜单.为了制作圆角,我使用了 Qt 样式表.这是我的 CSS

I'm creating a round context menu using QFrame. To make round corner, I used Qt style sheet. Here is my CSS

    this->setStyleSheet("QFrame#ShareContextMenu{background-color:rgb(255,255,255);
    border-width:1px;
    border-color :rgb(0,0,0);
    border-radius:10px;
    border-style:solid;}

    QPushButton{background-color:rgba(255,255,255,0);}
    QPushButton::hover{background-color:rgba(125,125,125,50); border-radius:5px;}");

如何去除这张图片中标有红色圆圈的白色背景?

How can I remove the white background marked with red circles in this picture?.

这是使用 QWidget::setMask() 的解决方案.在构造函数中添加以下代码

Here is the solution using QWidget::setMask(). Add the following codes inside constructor

    QPixmap px(this->size()); //Create pixmap with the same size of current widget
    px.fill(Qt::transparent); //Fill transparent
    QPainter p(&px);
    QBrush brush;
    brush.setStyle(Qt::SolidPattern); //For fill
    p.setBrush(brush);
    p.drawRoundedRect(this->rect(), 15.0, 15.0); //Draw filled rounded rectangle on pixmap
    this->setMask(px.mask()); //The the mask for current widget.

推荐答案

我认为您无法使用样式表解决问题.QMenu 是一个矩形的顶层小部件.

I think you cannot resolve the problem using style sheets. QMenu is a rectangular top-level widget.

this 是你的 QMenu 吗?如果是这样,试试这个:

Is this your QMenu? If so, try this:

this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);

用实例化的 QMenu 对象替换 this.

Replace this by your instantiated QMenu object.

当然,你也可以使用 setMask 来隐藏所需的区域.例如:

Of course, you could also use setMask to hide the required region. For example:

QRegion region (menu->x(),
                menu->y(),
                menu->sizeHint().width(),
                menu->sizeHint().height(),
                QRegion::Ellipse);
menu->setMask(region);

这篇关于QFrame圆形边框透明背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆