QFrame round border transparent background

帅比萌擦擦* 提交于 2020-07-19 06:47:04

问题


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?.

Edit:

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.

回答1:


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

Is this your QMenu? If so, try this:

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

Replace this by your instantiated QMenu object.

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);


来源:https://stackoverflow.com/questions/32491282/qframe-round-border-transparent-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!