我正在使用QFrame创建一个圆形上下文菜单。为了制作圆角,我使用了Qt样式表。这是我的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;}");
我怎样才能移除这幅画中标有红色圆圈的白色背景?
编辑:
下面是使用QWidget::setMask()的解决方案。在构造函数中添加以下代码
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.
发布于 2015-09-10 09:54:38
我认为您不能使用样式表解决这个问题。QMenu
是一个矩形顶层小部件。
this
是你的QMenu
吗?如果是这样,请尝试如下:
this->setWindowFlags(this->windowFlags() | Qt::FramelessWindowHint);
this->setAttribute(Qt::WA_TranslucentBackground);
用实例化的this
对象替换QMenu
。
当然,您也可以使用setMask隐藏所需的区域。例如:
QRegion region (menu->x(),
menu->y(),
menu->sizeHint().width(),
menu->sizeHint().height(),
QRegion::Ellipse);
menu->setMask(region);
https://stackoverflow.com/questions/32491282
复制相似问题