【发布时间】:2016-02-04 20:45:07
【问题描述】:
我想创建一个按钮来显示具有透明背景的给定图像。我正在使用下面的代码段:
ui->setupUi(this);
this->setFixedSize(width, height);
QPixmap* orgPixmap = new QPixmap(":/images/a.png");
QSize size(this->width(), this->height());
QPixmap resizePixmap(orgPixmap->scaled(size));
QPalette palette;
palette.setBrush(this->backgroundRole(), QBrush(resizePixmap));
this->setPalette(palette);
ui->toolButton->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
ui->toolButton->setFixedSize(64, 64);
ui->toolButton->setIconSize(QSize(64, 64));
ui->toolButton->setIcon( QIcon(":/images/add.png") );
在小部件的顶部,我添加了一个带有图标的按钮。这是一个带有透明背景的蓝色圆圈。但是,当我在 Android 设备上部署和运行我的代码时,按钮显示为带有白色的不透明背景。
请看图:
我尝试使用以下代码强制按钮透明:
QColor transparent_color(0,0,0,0);
QPalette button_palette(ui->pushButton->palette());
button_palette.setColor(QPalette::Button, transparent_color);
ui->toolButton->setPalette(button_palette);
上面的代码似乎没有达到我预期的效果。 我想知道问题可能是什么以及如何解决。
【问题讨论】:
-
你的解释不是很清楚。您希望透明小部件位于另一个小部件之上并且只显示图像?
-
对不起,我的英语水平不好。但你说的是我想要的东西
-
你检查过按钮上的
setAttribute(Qt::WA_TranslucentBackground, true);吗? -
我试过“ui->toolButton->setAttribute(Qt::WA_TranslucentBackground, true);”但它仍然无法按我的意愿工作