开发者

Is it possible to set the opacity of qt widgets?

开发者 https://www.devze.com 2023-01-31 20:07 出处:网络
I know that there is a function QWidget::setWindowOpacity(qreal level) but as written in the documentation this does only work for windows开发者_如何学运维.

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!


Just use QGraphicsOpacityEffect in order to achieve this effect.

  • Qt4: http://doc.qt.io/qt-4.8/qgraphicsopacityeffect.html
  • Qt5: http://doc.qt.io/qt-5/qgraphicsopacityeffect.html
  • Qt6: https://doc.qt.io/qt-6/qgraphicsopacityeffect.html


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)


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


mywidget.setStyleSheet('background-color:rgba(r, g, b, alpha);') 

works for me


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

Is it possible to set the opacity of qt widgets?

0

精彩评论

暂无评论...
验证码 换一张
取 消