Qt 使用颜色和 alpha-map 绘制图标

标签 qt icons alpha qpainter

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

结果应该是,当 alpha 贴图的不透明度为 0 并且应该绘制使用的颜色时,当不透明度为 1 时,背景上没有绘制任何内容。

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

有人有想法吗?

提前致谢。

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

enter image description here

最佳答案

你可以像这样使用 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);

这是我使用上面的代码(QPixmap.save() 方法)获得的灰度图像和两个彩色图标:
icons

关于Qt 使用颜色和 alpha-map 绘制图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24943711/

相关文章:

qt - 当鼠标悬停在 QPushButton 上时,如何从 QPushButton 发出信号?

c++ - 如何为 QSet<SomeClass*> 容器编写 qHash?

c++ - KDE 在控制台中写入(太多)

javascript - 在 Angular JS 中切换 ng-repeat 中的图标

c# - 正则表达式 - 仅匹配包含任何字母字符的字符串

qt - 如何将 cv::Mat 转换为 QImage 或 QPixmap?

html - 如果我有一个生成的图标字体作为 TTF,我可以告诉哪个 CSS 内容属性属于哪个字符吗?

android - 自适应图标显示不正确

html5-canvas - HTML5 canvas在StrokeStyle rgba/globalAlpha之间的性能

python - 使用非透明像素从 QImage 创建蒙版