2011-09-29 80 views
2

是否可以爲QWidget分配一個alpha透明掩碼?我知道如何設置使用setMask的面具,但它似乎只支持黑色&白色面具。是否有可能使其支持真正的Alpha通道?在QWidget上使用alpha透明遮罩?

即目前我有這樣一個PNG:

enter image description here

和小窗口是這樣的:

enter image description here

如果我打開我的PNG在的QPixmap,並將其設置爲面具,我得到這個(注意邊緣):

enter image description here

不過,我想獲得這個(平滑的邊緣):

enter image description here

任何想法如何做到這一點?

注意:我在小部件上做了一些更復雜的繪圖,它必須限制在遮罩區域,所以我不能簡單地將我的PNG設置爲小部件的背景圖像。

回答

2

我認爲你最好的路線是在QPainter's composition modes

例如:

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); 

將處理很好地繪製你的部件。如果你仍然需要屏蔽鼠標事件,你可能需要做一些額外的工作。