Filippo Micheletti
Filippo Micheletti

Reputation: 65

Set Qt Widget mask

I would like to have a button with the shape of its icon image in my QT application. I'm trying to set the button mask by obtaining the mask from the used image (.png) using the alpha channel.

Here are the interested code lines:

QPushButton button();
QPixmap pixmap("image_path.png");
QIcon icon(pixmap);
button.setIcon(icon);
button.setMask(pixmap.createMaskFromColor(Qt::transparent,Qt::MaskInColor));

The result is that the button disappears!

By doing some tests I'm sure that the mask is created rightly, where is the mistake?

Upvotes: 1

Views: 7070

Answers (1)

KaZ
KaZ

Reputation: 1163


if you're using a .png with alpha like this one :
transparent png
you can set its shape as a mask like this:

QPushButton button;
button.resize(50,50);
button.show();

QPixmap mask("D:/shape.png");
button.setMask(mask.mask());

You will got a result like :
result shape button

Upvotes: 4

Related Questions