Not getting correct blending when using QOpenGLWidget and a translucent window
-
wrote on 28 Aug 2024, 23:49 last edited by Guy Gizmo
I'm trying to create a translucent window with a
QGraphicsScene
using OpenGL as its backend, using Qt 6.2.3 in macOS 13.I've got images rendering into it, but transparency is not working correctly. The window is indeed translucent, and shapes that I add to the
QGraphicsScene
with an alpha value of less than one are partially transparent, but rather than doing a simple alpha blend, it does a blend that seems equivalent to Photoshop's "screen" blending, where dark colors take on the color of the layer being blended into it, and light colors are unaffected.I've tried setting the correct blending function using
glBlendFunc
in both thepaintGL
method of myQOpenGLWidget
and in myQGraphicView
drawBackground
method, but it doesn't have any effect.To give an illustration of the sort of wrong blending I'm getting, here's what happens if open my translucent window over a browser window, and I call
glClear
in mydrawBackground
method with a clear color of(1.0, 0.0, 0.0, 0.0)
, a color which ought to be 100% transparent but instead has this effect:What do I need to do to just get regular alpha blending?
-
wrote on 29 Aug 2024, 15:40 last edited by
I've sorted it out: I had a feeling this might have to do with premultiplied alpha, and it turns out that the macOS compositor requires all windows to premultiply their alpha before compositing. All I needed to do was change my calls to
glColor4f
so that the red, green, and blue values were multiplied by the alpha value. I'll also need to make sure that any textures I use are premultiplied. -
wrote on 29 Aug 2024, 15:32 last edited by
I've done a bit more digging into this, and I've found that this issue happens without using
QGraphicsView
, so that aspect of this is irrelevant. I can reproduce the issue by just using aQOpenGLWidget
in a translucent window.Any drawing I perform using
QPainter
in the widget'spaintGL
method will have correct transparency and blend properly with the windows beneath it. However, any drawing I do between calls toQPainter::beginNativePainting()
andQPainter::endNativePainting()
with pure GL calls will have this transparency issue. I'm still not sure why, though! -
wrote on 29 Aug 2024, 15:40 last edited by
I've sorted it out: I had a feeling this might have to do with premultiplied alpha, and it turns out that the macOS compositor requires all windows to premultiply their alpha before compositing. All I needed to do was change my calls to
glColor4f
so that the red, green, and blue values were multiplied by the alpha value. I'll also need to make sure that any textures I use are premultiplied. -
1/3