I'm creating an application, which interacts with OpenGL via *QOpenGL\** classes. The graphics is shown through a *QOpenGLWidget*, which is placed in a UI-form. 

Now, there is a library for CAD purposes ([Open CASCADE](https://www.opencascade.com)), an OpenGL interface of which requires a handle to the render window. The question is: can I somehow say the library to render everything to the mentioned widget? 

In other words, is there a way to interpret the widget as a native, probably, platform-specific (HWND here) window, so that the library renders its own stuff exactly there?

Thanks
['https://Stackoverflow.com/questions/55715000', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4886665/']
**QOpenGLWidget** is not the same thing as **QGLWidget**.

The classical approach for embedding OCCT 3D viewer, which you can find in Qt IESample coming with OCCT, creates QWidget with unique window handle flag, takes this window handle and ask OCCT to take care about OpenGL context creation for this window. This is most straightforward, robust and portable way with one limitation - Qt will not be able drawing semitransparent widgets on top of this QWidget. This is not a limitation of OCCT, but rather limitation of Qt Widgets design.

**QOpenGLWidget** was intended to solve this limitation by allowing to mix custom OpenGL rendering and normal widgets. The integration of external OpenGL graphics engine, however, became more complicated and fragile. It is not very helpful stealing winId() from QOpenGLWidget, as rendering content is expected to be drawn not into window itself, but rather into OpenGL framebufer object (FBO), created by QOpenGLWidget - see **QOpenGLWidget::defaultFramebufferObject()** property.

External renderer is expected to render into this FBO for proper composition of Qt widgets. Luckily, OCCT is flexible enough to allow such integration. Unluckily, such integration requires some knowledge of OpenGL, as well as its usage by Qt and OCCT.
For that, you need to ask OCCT wrapping OpenGL context already created by Qt (for that **V3d\_View::SetWindow()** provides an optional argument of type *Aspect\_RenderingContext*, which corresponds to **HGLRC** on Windows and can be fetched using **wglGetCurrentContext()** within rendering thread) as well as FBO created by QOpenGLWidget (for that, OCCT provides **OpenGl\_FrameBuffer::InitWrapper()** and **OpenGl\_Context::SetDefaultFrameBuffer()** methods, as well as **OpenGl\_Caps::buffersNoSwap** flag to leave window buffer swapping management to Qt).

OCCT doesn't come yet with a sample using **QOpenGLWidget**, but you can find also **qt/AndroidQt** sample implementing similar thing for embedding OCCT 3D Viewer into **QtQuick** application.
After some investigation, I found that method *QOpenGLWidget::winId()* returns the correct handle. It's been found out only now, because the rendered scene disappeared immediately, leaving a black picture instead. However, when the viewport is resized, the scene returns back (and disappears again, though). Looks like Open CASCADE has problems with Qt 5 OpenGL implementation, since QGLWidget didn't have such problems, as far as I know.