Qt使用颜色和Alpha贴图绘制图标

ruh*_*ner 7 icons qt alpha qpainter

我想以不同的颜色绘制图标(仅一种颜色)。为此,我想导入单个alpha纹理,然后在应用程序中将其与给定的颜色组合。

结果应该是,当alpha贴图的不透明度为0时,什么都不会绘制到背景上;而当不透明度为1时,应该绘制使用的颜色。

一种解决方案应该隐藏在中的某个位置QPainter,因为您可以手动设置Composition-Mode(QPainter::setCompositionMode)。但是我没有让它像我想要的那样工作。

有人有主意吗?

提前致谢。

编辑:这是一张小图,解释了我想做什么。我想使用如图所示的Alpha-Map,然后使用颜色图层创建我的图标。重要的是,背景保持透明。

在此处输入图片说明

小智 6

您可以像这样使用QPainter进行操作:

QColor color;
// set color value

// load gray-scale image (an alpha map)
QPixmap pixmap = QPixmap(":/images/earth.png");

// initialize painter to draw on a pixmap and set composition mode
QPainter painter(&pixmap);
painter.setCompositionMode(QPainter::CompositionMode_SourceIn);

painter.setBrush(color);
painter.setPen(color);

painter.drawRect(pixmap.rect());

// Here is our new colored icon!
QIcon icon = QIcon(pixmap);
Run Code Online (Sandbox Code Playgroud)

这里是灰度图像和两个彩色图标我开始使用上述(QPixmap.save()方法)的代码: 图标

  • 出于某种原因,我不得不用`painter.fillRect(pixmap.rect(), color)`(在python中)替换设置的画笔、笔和drawRect线。更少的代码和工作,这很好。我也有`painter.end()`,但不确定是否需要。 (2认同)