Using an alpha transparent mask on a QWidget?

眉间皱痕 提交于 2019-12-02 03:41:19

问题


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:

and a widget like this:

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

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/7594066/using-an-alpha-transparent-mask-on-a-qwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!