Not getting correct blending when using QOpenGLWidget and a translucent window
-
I'm trying to create a translucent window with a
QGraphicsSceneusing 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
QGraphicsScenewith 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
glBlendFuncin both thepaintGLmethod of myQOpenGLWidgetand in myQGraphicViewdrawBackgroundmethod, 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
glClearin mydrawBackgroundmethod 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?
-
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
glColor4fso 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. -
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 aQOpenGLWidgetin a translucent window.Any drawing I perform using
QPainterin the widget'spaintGLmethod 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! -
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
glColor4fso 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. -
G Guy Gizmo has marked this topic as solved on