2015-02-11 235 views
3

我如何將QFrame的背景設置爲半透明,以便我可以看到父窗口小部件(QWidget)的背景。我試過,但背景保持黑色:Qt css背景半透明

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

你嘗試加入'的setAttribute(Qt的:: WA_TranslucentBackground);' – 2015-02-11 09:04:52

+0

你如何使用時,這樣做QT設計師? – Michael 2015-02-11 09:05:49

+0

在您的構造函數中執行此操作 – 2015-02-11 09:14:24

回答

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 2015-02-11 11:49:17

+0

我已編輯上面的代碼.... – 2015-02-11 12:00:58