在Linux中的QT C ++中设置QButton的透明度

问题描述 投票:1回答:2

我正在使用基于Qt 5.7.0的Qt Creator 4.0.2(GCC 4.9.1 20140922(Red Hat 4.9.1-10),64位)。

我正在小部件上显示图像,并在其顶部放置QButtonsQLabels

看起来像这样:

Window with image and button

我想使按钮半透明。没有任何属性。我尝试在另一个线程上给出答案:

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

但是它对我没有用。我也尝试过setStyleSheet("background:transparent;")但这没有用。

请提出一种解决方法。

c++ qt qtstylesheets qpushbutton
2个回答
0
投票

为按钮设置透明背景是不够的:您必须为主窗口小部件设置一些属性,以要求窗口管理器让按钮管理其自己的背景。

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

0
投票

仅设置bg颜色是不够的,您需要设置“ border-style:outset;”在主窗口/窗口中没有其他内容...

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.