C++ 是否可以设置 qt 小部件的不透明度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4499488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to set the opacity of qt widgets?
提问by Exa
I know that there is a function QWidget::setWindowOpacity(qreal level)
but as written in the documentation this does only work for windows.
我知道有一个函数,QWidget::setWindowOpacity(qreal level)
但正如文档中所写的那样,它只适用于 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!
提前致谢!
回答by Miguel Fuentes
Just use QGraphicsOpacityEffect in order to achieve this effect.
只需使用 QGraphicsOpacityEffect 即可实现此效果。
- Qt4: http://doc.qt.io/qt-4.8/qgraphicsopacityeffect.html
- Qt5: http://doc.qt.io/qt-5/qgraphicsopacityeffect.html
- Qt4:http: //doc.qt.io/qt-4.8/qgraphicsopacityeffect.html
- Qt5:http://doc.qt.io/qt-5/qgraphicsopacityeffect.html
回答by Ja8zyjits
Well for widgets inside mainwidow appear to have setAutoFillBackground(False)
by default.
对于 mainwidow 中的小部件来说setAutoFillBackground(False)
,默认情况下似乎有。
to make it fade in fadeout u need to to use QGraphicsOpacityEffect
along with setAutoFillBackground(True)
要使其淡入淡出,您需要QGraphicsOpacityEffect
与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)
回答by Pawel Zubrycki
SetWindowOpacity
works for me in Linux. I used code like this to change window opacity, (value is from 0 to 100):
SetWindowOpacity
在 Linux 中对我有用。我使用这样的代码来更改窗口不透明度,(值从 0 到 100):
setWindowOpacity(qreal(value)/100);
回答by Petr Tripolsky
In Qt5 you can use css to make widgets transparent
在 Qt5 中,您可以使用 css 使小部件透明
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();
}
回答by MACRO DAT
mywidget.setStyleSheet('background-color:rgba(r, g, b, alpha);')
works for me
为我工作