1

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

I am displaying an image on my widget and placing QButtons and QLabels on top of it.

It looks something like this:

Window with image and button

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.

Please suggest a way of doing it.

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
A.k.
  • 192
  • 1
  • 22
  • If you are using widget app., why dont you use interface(forms->mainwindow.ui) property part to change it to transparent. Or you can check [this post](https://stackoverflow.com/questions/21685414/qt5-setting-background-color-to-qpushbutton-and-qcheckbox) – Yunus Temurlenk Jan 17 '20 at 05:38
  • @YunusTemurlenk Like I said, "There isn't any property for it" to set the QButton to transparent. Also, the link you shared talks about changing background-color, not transparency. – A.k. Jan 17 '20 at 05:50
  • If you change background color as "transparent", it will make it transparent. In qt "transparent" is also a color. – Yunus Temurlenk Jan 17 '20 at 06:00
  • `background-color: transparent` just make it black. – A.k. Jan 17 '20 at 06:09

2 Answers2

2

setting the bg color to "transparent" is not enough, you need to set the "border-style: outset;" and nothing else in the main window/widget...

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

enter image description here

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

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
  • It worked as expected. Can we set the opacity for the button, possibly with any color? As my main question was "I want to make the button semi-transparent." – A.k. Jan 17 '20 at 07:57
  • Got it. I just had to add `background: rgba(0, 0, 0, 0.5); `. Accepting your answer. – A.k. Jan 17 '20 at 08:05
0

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();
Dimitry Ernot
  • 6,256
  • 2
  • 25
  • 37