Qt CSS背景半透明

3

我该如何将QFrame的背景设置为半透明,以便可以看到父窗口(QWidget)的背景。我尝试了以下方法,但背景仍然是黑色:

QFrame {    
    margin: 10px;
    background: #000000;
    border: 1px solid black;
    opacity: 0.5;
}

当使用QT Designer时,您如何做到这一点? - Michael
在你的构造函数中完成这个任务。 - Thomas Ayoub
那也不行。 - Michael
很抱歉,我现在无法提供更多帮助。 - Thomas Ayoub
感谢您的帮助。 - Michael
显示剩余3条评论
1个回答

0
请尝试这个...
Widget::Widget(QWidget *parent) : QWidget(parent)
{
    setWindowFlags(Qt::Window | Qt::FramelessWindowHint);

    setAttribute(Qt::WA_NoSystemBackground);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_PaintOnScreen);

    setAttribute(Qt::WA_TransparentForMouseEvents);
}

void Widget::paintEvent(QPaintEvent*)
{
    QPainter p(this);
    QString text = "Some foo goes here";
    QFontMetrics metrics(p.font());
    resize(metrics.size(0, text));
    p.drawText(rect(), Qt::AlignCenter, text);
}

1
它需要是半透明的。 - Michael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接