简体   繁体   中英

Transpaprent QLabel

#include <QtGui>

class   Label : public QLabel
{
public:
    Label(QWidget *parent =0) :
        QLabel(parent)
    {
        resize(100, 100);
        setText("hello");
        show();
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Label l;

    return a.exec();
}

This outputs a label 'hello' with a background. I'm using Qt4. I want to make the background of this label completely transparent. But setWindowOpacity changes the whole widget transparency. I want the content as it is, but only the background to be transparent.

我发现这更简单....

QWidget::setAttribute(Qt::WA_TranslucentBackground);

您可以使用样式表设置背景的颜色和Alpha值:

setStyleSheet("background-color: rgba(0,0,0,0%)");

In PyQt:

lbl.setAttribute(Qt.WA_TranslucentBackground, True)

In my case with Qt5, the following worked:

movieLabel->setAutoFillBackground(false);

Tested both with Qt 5.11.1 and Qt 5.8.0.

If you define a QColor with alpha of 0 you will get background with transparent color, so for example:

QColor bg_color(255, 0, 0, 0);
QPalette p(l.palette());
p.setColor(QPalette::BackgroundColor, bg_color);
l.setPalette(p);

Which should make label's background whatever color transparent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM