Transparent QGLWidget on top of QGraphicsView
-
26-09-2019 - |
Question
I'm using QGraphicsView
to show a 2D image and also have a separate QGLWidget
window to display some 3D object. I'm dynamically changing the image displayed in `QGraphicsView' based on the rotation of the 3D object.
I would like to render a semi-transparent 3D object on top of the 2D image, something like Maya 2009 used to do (notice the cube in the upper right corner of the viewport):
(source: boulevard-creation.com)
Is it possible to do this with my current widgets? If not, how could it be done?
One option I can think of would be to render everything in QGLWidget
and display the 2D image as a texture on a background plane, but that seems slightly painful.
Solution
I have found a way, but it is quite slow: you can simply add another QGraphicsItem
to the QGraphicsScene
like this
scene->addPixmap(glWidget->renderPixmap());
Transparency can than be set with QGraphicsItem::setOpacity()
.
There should be a faster way using QPixelBuffer
, but I didn't manage to render to it properly yet.
OTHER TIPS
Set the viewport of your qgraphicsview to the qglwidget, then in your paint method you can directly use gl calls to draw your 3d objects.
See http://doc.qt.io/archives/qt-4.7/qpainter.html#beginNativePainting