Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Pixmap Mask not coloring over the image

Pixmap Mask not coloring over the image

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 5.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joshua-anderson
    wrote on last edited by
    #1

    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());
    p.end();
    QIcon maximizeButtonIcon(pix);
    ui->pushButton->setIcon(maximizeButtonIcon);@

    1 Reply Last reply
    0
    • C Offline
      C Offline
      chris17
      wrote on last edited by
      #2

      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());
      newPix.fill(Qt::transparent);
      QBitmap mask=pix.createMaskFromColor(Qt::black,
      Qt::MaskOutColor);
      QPainter p(&newPix);
      p.setRenderHint(QPainter::SmoothPixmapTransform);
      p.setRenderHint(QPainter::Antialiasing);

      p.setBackgroundMode(Qt::TransparentMode);
      p.setPen(color);
      p.drawPixmap(newPix.rect(),mask,pix.rect());
      p.end();
      
      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.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        joshua-anderson
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          chris17
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • mranger90M Offline
            mranger90M Offline
            mranger90
            wrote on last edited by
            #5

            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 ?

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved