Pixmap Mask not coloring over the image

  • Hello!

    I'm trying to use a mask over a qpixmap to change the color of a single black icon, like this !http://puu.sh/4taEq.png(icon)! . I'm trying to use a qpixmap with a qpainter to color over it. My code throws no errors, but the icon's color isn't changed. I'm guessing it is a problem with the refrence but trying any other way will just throw a error. Anybody see what is wrong in my code?

    @QPixmap pix ("C:/black.png");
    QBitmap mask = pix.createMaskFromColor(QColor(255, 255, 255), Qt::MaskOutColor);
    QPainter p (&pix);
    p.setPen(QColor(0, 0, 255));
    p.drawPixmap(pix.rect(), mask, mask.rect());
    QIcon maximizeButtonIcon(pix);

  • Had to do the same thing a while ago.

    This works for me:
    QPixmap colorPixmap(const QPixmap &pix, const QColor &color)
    QPixmap newPix(pix.size());
    QBitmap mask=pix.createMaskFromColor(Qt::black,
    QPainter p(&newPix);

    return newPix;


    This colors all black stuff of pix with the given color.
    Not much difference to your code, maybe you should draw on a new pixmap.

  • Your code works great! However, it overwrites the alpha of the existing color. This is bad, because we are using images like a close button (!http://puu.sh/4tZlR.png(X)!). The problem is that the X looks all pixely without the antialising alpha values. I suspected this might happen but was hoping that either the QPainter::Antialiasing would prevent it or that QPaint with only a RGB would not touch the alpha chanel. Do you have any advice on this? Do I have to write some code that iterates through every pixel to manually change the color?

  • Sorry, I didn't see that your use case is very different to mine. In my case I just take a black and white image and color all black pixels with the wanted color, that's why I create a new pixmap with transparent background.

    Could you show an image of how the result should look like?

  • I'm could be wrong, but isn't you mask incorrect ?
    255,255,255 would refer to all white. If you want to
    mask black then it should be 0,0,0 ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.