2
votes

I am trying to do something that to my surprise was much harder than first anticipated. I have a small bitmap resource that is 32bit png that I would like to display as an icon after tinting it to a specific color.

I came across QGraphicsEffect class and its children but it wont work unless I set up a whole scene graph which is kind of overkill for my purpose (rendering a 16x16 icon in different colors).

I also came across QPixmapFilter class in Qt4 but I can't find any reference for it in Qt5. Was it removed?

Also there is clearly some filters working on icons as seen in the widget icons example with both tints and blurs going on.

So how can I accomplish this simple task in Qt5?

1

1 Answers

1
votes

Until a better answer arrives I decided to share the approach I ended up using. It's not IMO a very elegant solution, but it works. It preserves transparency and I have tested it to work on SVG resources as well.

Tint icon function for Qt5:

#include <QPainter>
#include <QPixmap>
#include <QImage>
#include <QGraphicsEffect>

QImage tint(QImage src, QColor color, qreal strength=1.0){
    if(src.isNull()) return QImage();
    QGraphicsScene scene;
    QGraphicsPixmapItem item;
    item.setPixmap(QPixmap::fromImage(src));
    QGraphicsColorizeEffect effect;
    effect.setColor(color);
    effect.setStrength(strength);
    item.setGraphicsEffect(&effect);
    scene.addItem(&item);
    QImage res(src);
    QPainter ptr(&res);
    scene.render(&ptr, QRectF(), src.rect() );
    return res;
}

Usage:

QIcon ic(QPixmap::fromImage(tint(QImage("://icons/spider.svg"),QColor(192,0,0),1.0)));

This produces a readily tinted QIcon instance from arbitrary graphics resource.