开发者

Using an alpha transparent mask on a QWidget?

Is it possible to assign an alpha-transparent mask to a QWidget? I know how to set a mask using setMask but it seems it only supports black&white masks. Is it possible to make it support a true alpha channel?

i.e. currently I have a PNG like this:

Using an alpha transparent mask on a QWidget?

and a widget like this:

Using an alpha transparent mask on a QWidget?

If I load my PNG in a QPixmap and set it as a mask, I get this (notice the edges):

Using an alpha transparent mask on a QWidget?

However I would like to get this (smooth edges):

Using an alpha transparent mask on a QWidget?

开发者_StackOverflow社区

Any idea how to do that?

Note: I'm doing some more complex drawing on the widget, which must be restricted to the mask area, so I cannot simply set my PNG as the widget's background image.


I think your best route is in QPainter's composition modes.

For example:

QPixmap PixmapToBeMasked(Size);
PixmapToBeMasked.fill(QColor(255, 255, 255, 120));

QPixmap Mask = DoSomethingToGetAMask();

QPainter Painter(&PixmapToBeMasked);
Painter.setCompositionMode(QPainter::CompositionMode_DestinationIn);
Painter.drawPixmap(0, 0, Mask.width(), Mask.height(), Mask);

That will handle drawing your widget nicely. If you still need to mask mouse events you might need to do some extra work though.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜