简体   繁体   English

在 Linux 中的 QT C++ 中设置 QButtons 的透明度

[英]Set transparency of QButtons in QT C++ in Linux

I am using Qt Creator 4.0.2 Based on Qt 5.7.0 (GCC 4.9.1 20140922 (Red Hat 4.9.1-10), 64 bit).我正在使用基于 Qt 5.7.0(GCC 4.9.1 20140922(Red Hat 4.9.1-10),64 位)的 Qt Creator 4.0.2。

I am displaying an image on my widget and placing QButtons and QLabels on top of it.我在我的小部件上显示一个图像,并在它上面放置QButtonsQLabels

It looks something like this:它看起来像这样:

带有图像和按钮的窗口

I want to make the button semi-transparent.我想让按钮半透明。 There isn't any property for it.没有它的任何财产。 I tried the answer given on another thread as:我尝试了另一个线程上给出的答案:

ui->button->setStyleSheet("background-color: rgba(255, 255, 255, 50);");

But it didn't work for me.但它对我不起作用。 I also tried setStyleSheet("background:transparent;") But it didn't work.我也试过setStyleSheet("background:transparent;")但它没有用。

Please suggest a way of doing it.请建议一种方法。

setting the bg color to "transparent" is not enough, you need to set the "border-style: outset;"将bg颜色设置为“透明”是不够的,还需要设置“border-style:outset;” and nothing else in the main window/widget...主窗口/小部件中没有其他内容...

ui->pushButton_7->setStyleSheet("background-color: transparent; style: outset;");

在此处输入图片说明

then:然后:

do:做:

ui->pushButton_7->setStyleSheet("background-color: #FF0011");
auto qgoEffect = new QGraphicsOpacityEffect(this);
qgoEffect->setOpacity(0.15);
ui->pushButton_7->setGraphicsEffect(qgoEffect);
ui->pushButton_7->setAutoFillBackground(true);

在此处输入图片说明

Setting a transparent background to the button is not enough: you have to set some attributes to your main widget in order to ask the window manager to let the button to manage its own background.为按钮设置透明背景是不够的:您必须为主小部件设置一些属性,以便要求窗口管理器让按钮管理自己的背景。

QWidget* w = new QWidget();

QPushButton* btn = new QPushButton("Click me", w);
btn->setStyleSheet("background-color: rgba(255, 255, 255, 50);");

w->setWindowFlags(Qt::FramelessWindowHint); // Do not display the window frame
w->setAttribute( Qt::WA_TranslucentBackground, true ); // Do not display the default background

w->show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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