C++ over Qt : Controlling transparency of Labels and Buttons

寵の児 提交于 2019-11-27 02:23:37

问题


Well, I was again trying my hands on a Linux GUI app on Qt Creator, I added couple of images in a Qt resource file of my project. And I tried to have a nice background in my main window and other windows and dialogs. I was using from the stylesheets option (no coding).

I am unable to set the transparency level of labels and pushbuttons. Any ideas on how to do it from Qt creator GUI itself ???
!I am attaching a snap of how my application looks.


回答1:


You can set transparency of QLabel or QPushbutton by setting the stylesheet :

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

You can also add background-color: rgba(255, 255, 255, 0); to the styleSheet property of the widget in the designer.

The fourth parameter is alpha. You can also have semi-transparent widgets by setting alpha to some value more than zero :

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



回答2:


There is the property "Window opacity" in the QWidget section of the ui element property (bottom right in qtDesigner view). By default it is 1.0 (completely opaque).

It is also available programmatically




回答3:


This worked For me:

this->setWindowOpacity(0.35);
this->setAttribute(Qt::WA_TranslucentBackground, false);
this->setStyleSheet("background-color: yellow;");



回答4:


Not from GUI, but still can be useful for someone:

    auto effect = new QGraphicsOpacityEffect(this);
    effect->setOpacity(0.5);
    yourWidget->setGraphicsEffect(effect);
    yourWidget->setAutoFillBackground(true);

Stolen from here.



来源:https://stackoverflow.com/questions/23948453/c-over-qt-controlling-transparency-of-labels-and-buttons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!