Is it possible to set the opacity of qt widgets?
Asked Answered
P

5

13

I know that there is a function QWidget::setWindowOpacity(qreal level) but as written in the documentation this does only work for windows.

Is there a way to make widgets that are lying inside layouts opaque too?

What I'm trying to do is an animation where widgets are fading in. I once did that with a preferences-dialog and there it worked.

So do you think there is a way or a work-around to achieve opacity for widgets inside layouts? How would you do that?

Thanks in advance!

Parma answered 21/12, 2010 at 13:0 Comment(2)
If you have no background under widget, maybe you can change widget color starting from background color.La
There is a similar question Animate transparency.. with a code example.Akkad
B
14

Just use QGraphicsOpacityEffect in order to achieve this effect.

Bethlehem answered 12/1, 2011 at 14:46 Comment(0)
B
12

Well for widgets inside mainwidow appear to have setAutoFillBackground(False) by default.

to make it fade in fadeout u need to to use QGraphicsOpacityEffect along with setAutoFillBackground(True)

a small example: write inside the widget which is called inside the mainwindow

op=QGraphicsOpacityEffect(self)
op.setOpacity(1.00) #0 to 1 will cause the fade effect to kick in
self.setGraphicsEffect(op)
self.setAutoFillBackground(True)
Beefy answered 21/3, 2015 at 7:46 Comment(1)
Setting setAutoFillBackground on a child of a central widget did the trick for me: opacity effect now works for the widget. Any idea why setting that is required?Vally
W
4

SetWindowOpacity works for me in Linux. I used code like this to change window opacity, (value is from 0 to 100):

setWindowOpacity(qreal(value)/100);
Westcott answered 21/12, 2010 at 14:22 Comment(2)
This works for windows, but not for widgets which are part of a layout etc. At least for me, I am using Microsoft Windows.Parma
Ah, I misunderstanded. I though you mean MS Windows xDWestcott
T
3
mywidget.setStyleSheet('background-color:rgba(r, g, b, alpha);') 

works for me

Trustworthy answered 17/3, 2020 at 4:3 Comment(1)
Please look at formatting the code in your answer to make it easier to read.Merkley
S
0

In Qt5 you can use css to make widgets transparent

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QDialog dialog;
    dialog.setStyleSheet(QLatin1String("#LolButton{color: transparent; background-color: transparent;}"));
    QPushButton button(&dialog);
    button.setText("Button");
    button.setObjectName(QStringLiteral("LolButton"));
    QObject::connect(&button,&QPushButton::clicked,[](){
        QMessageBox msg;
        msg.setText("LolButton omg");
        msg.exec();
    });
    dialog.show();
    return a.exec();
}

enter image description here

Salvador answered 1/1, 2018 at 18:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.