0

I'm making a simple puzzle game. The piece is a square and it contains the corresponding part of an image. What I need to do is to change the shape from the square to the common puzzle shape. I've made a template in Photoshop:

Corner piece

Now I need somehow to use it as a mask to change the shape of my square piece. I tried the following:

QImage temp;
temp.load("://Templates/c1.png");
temp = temp.convertToFormat(QImage::Format_Mono);
QBitmap bit;
bit = QBitmap::fromImage(temp);
puz->img.setMask(bit);

Where puzzle->img is my square piece. It works, but I get "rough corners" in the connectors. I suppose all of half-transparent pixels become fully filled with a color (or fully transparent?), so that's why it doesn't look smooth.

Is there a way to make it smoothier?

UPDATE: I tried a different approach: since setAlphaChannel() is an obsolete function, documentation says that I need to use QPainter::CompositionMode. I tried the following

void puzzle::paint(QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget )
{
   painter->setCompositionMode(QPainter::CompositionMode_Source);
   painter->fillRect(x, y, 90, 90, Qt::transparent);
   painter->setCompositionMode(QPainter::CompositionMode_SourceOver);
   painter->drawPixmap(x, y, 90, 90, img);
   painter->setCompositionMode(QPainter::CompositionMode_DestinationIn);
   painter->drawImage(x,y, mask);
}

It works like that in Image Composition example, but I get black pixels instead of transparent in my project: Output. Can't figure out, what's wrong. mask initialization:

mask.load("://Templates/c1.png");
mask = mask.scaled(90, 90, Qt::KeepAspectRatio, Qt::SmoothTransformation);
mask = mask.convertToFormat(QImage::Format_ARGB32_Premultiplied);
Vofel
  • 1
  • 3

1 Answers1

0

You should use setAlphaChannel instead. A mask only has values of 0 and 1 while the alpha channel should allow 256 variations in transparency.

Jay
  • 636
  • 6
  • 19
  • Thanks for the answer. Documentation doesn't suggest to use `setAlphaChannel`. I tried another approach, but still have some problems (see UPDATE: section). – Vofel Oct 28 '18 at 10:50
  • Have you tried this - https://stackoverflow.com/questions/7594066/using-an-alpha-transparent-mask-on-a-qwidget#7594160 ? – Jay Oct 28 '18 at 12:55
  • It's similar to what I've tried. If I add `painter.fillRect(resultImage.rect(), Qt::white);` (or any other color) with `QPainter::CompositionMode_DestinationOver` at the end of `void puzzle::paint`, it works fine - I see my piece with my template shape, the rest is filled with chosen color. However, if I use `Qt::transparent` it shows as black. – Vofel Oct 28 '18 at 17:06