Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 显示图像的透明按钮_Android_C++_Qt - Fatal编程技术网

Android 显示图像的透明按钮

Android 显示图像的透明按钮,android,c++,qt,Android,C++,Qt,我想创建一个按钮,用透明的背景显示给定的图像。我正在使用下面的代码段: 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)); Q

我想创建一个按钮,用透明的背景显示给定的图像。我正在使用下面的代码段:

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") );
在小部件的顶部,我添加了一个带有图标的按钮。这是一个背景透明的蓝色圆圈。然而,当我在安卓设备上部署并运行代码时,按钮显示为白色不透明背景

请看图片:

我已尝试使用以下代码强制按钮透明:

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);
上面的代码似乎没有达到我预期的效果。
我想知道问题可能是什么以及如何解决。

我知道您需要一个圆形按钮,那么
QWidget::setMask()怎么样


只是一个想法,我没有对此进行测试。

您可以设置一个简单的样式表。类似这样:
widget->setStyleSheet(rgba(255,255,255,0);”;
如果您想调整不透明度,只需更改第四个参数。

我最近在基于Qt Graphics View Framework的Win/Mac项目上解决了类似问题,请注意我从未在Android平台上测试过

无论如何,首先需要为图形场景设置ForeGroundBrush():

ui.yourGraphicsView->scene()->setsetForegroundBrush(Qt::transparent);
或者,您可以使用Qt Designer获得类似的结果,或按如下方式编辑Project.ui文件:

<widget class="QGraphicsView" name="yourGraphicsView">
...
 <property name="foregroundBrush">
  <brush brushstyle="NoBrush">
   <color alpha="255">
    <red>0</red>
    <green>0</green>
    <blue>0</blue>
   </color>
  </brush>
 </property>
我发现这两个选项足以让透明按钮悬停在QGraphicsScene中的其他对象上;调用
setMask()
并不是必需的,因为Qt会自动为任何QGraphicsPixmapItem(或祖先)调用当它设置为alpha通道PNG时。对于按钮,它的工作原理与您预期的一样:单击PNG的透明区域(上面显示白色)不会触发OnClick()事件,而单击基于PNG的按钮上的透明部分则会触发OnClick()事件


希望这有帮助……

您的解释不是很清楚。您希望透明小部件位于其他小部件之上,并且只显示图像?对不起,我的英语水平不好。但是您所说的我想让您检查
setAttribute(Qt::WA_transparcentbackground,true)
在按钮上?再看看这个问题:我试过“ui->toolButton->setAttribute(Qt::WA_TranslucentBackground,true)”;“但它仍然没有像我想要的那样工作
<widget class="QGraphicsView" name="yourGraphicsView">
...
 <property name="foregroundBrush">
  <brush brushstyle="NoBrush">
   <color alpha="255">
    <red>0</red>
    <green>0</green>
    <blue>0</blue>
   </color>
  </brush>
 </property>
ui->toolButton->setBackgroundBrush(Qt::transparent);