# OpenGL state errors with QVTKOpenGLWidget and vtk8.2

**URL:** https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539
**Category:** Support
**Created:** [March 19, 2019, 9:52pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539 "2019-03-19T21:52:21Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![geoffw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://discourse.vtk.org/u/geoffw)
#### Post date: [March 19, 2019, 9:52pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/1 "2019-03-19T21:52:21Z")

</div>

Hi,

We have an application based on Qt and Vtk. It runs well on VTK8.1 However, after upgrading to VTK8.2, we experience problems when we first call vtkRenderer::Render(). This call ultimately ends up calling vtkOpenGLState::CheckState, and this fuction emits a lot of error messages which are listed below.

I understand that the vtkOpenGLState is not initialized properly, but I can’t seem to figure out what the proper order of initialization is supposed to be. I am calling QSurfaceFormat::setDefaultFormat() before creating my QApplication object. The render window initialization is pretty straightforward:

\_vtkWidget = new QVTKOpenGLWidget((QWidget \*)nullptr);

\_vtkWidget-\>setEnableHiDPI(true);

// VTK Renderer

\_renderer = vtkSmartPointer\<vtkRenderer\>::New();

\_renderer-\>SetLayer(0);

\_rendererOverlay = vtkSmartPointer\<vtkRenderer\>::New();

\_rendererOverlay-\>SetLayer(1);

// VTK/Qt wedded

vtkNew\<vtkGenericOpenGLRenderWindow\> renderWindow;

\_qvtkWidget\>SetRenderWindow(renderWindow);

renderWindow-\>SetNumberOfLayers(2);

renderWindow-\>AddRenderer(\_renderer);

renderWindow-\>AddRenderer(\_rendererOverlay);

The vtkOpenGLState::Initialize function is never called

I have tried switching from QVTKOpenGLWidget to QVTKOpenGLNativeWidget, which fixes the state initialization problems (but I am unable to use it, since it causes problems with line drawing as documented here: [https://gitlab.kitware.com/vtk/vtk/issues/17154](https://gitlab.kitware.com/vtk/vtk/issues/17154)).

So the code works with 8.1 and also works with QVTKOpenGLNativeWidget.

Can someone help me understand what I am doing wrong with the initialization of QVTKOpenGLWidget or the render window?

```
Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 95

Error in cache state for GL_DEPTH_WRITEMASK

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 106

Error in cache state for GL_COLOR_WRITEMASK

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 124

Error in cache state for GL_CULL_FACE

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 131

Error in cache state for GL_MULTISAMPLE

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 144

Error in cache state for GL_STENCIL_TEST

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 162

Error in cache state for GL_VIEWPORT

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 173

Error in cache state for GL_SCISSOR_BOX

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 179

Error in cache state for GL_CULL_FACE_MODE

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 185

Error in cache state for GL_DEPTH_FUNC

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 191

Error in cache state for GL_BLEND_SRC_RGB

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 197

Error in cache state for GL_BLEND_SRC_ALPHA

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 203

Error in cache state for GL_BLEND_DST_RGB

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 209

Error in cache state for GL_BLEND_DST_ALPHA

Generic Warning: In d:\projects\flow-2\tools\conan\thirdparty\build\libvtk\rendering\opengl2\vtkopenglstate.cxx, line 222

Error in cache state for GL_COLOR_CLEAR_VALUE
```

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 20, 2019, 12:11am UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/2 "2019-03-20T00:11:54Z")

</div>

Is your widget in a scrolling region? You may be able to use the non-native widget depending on what parent widgets it is placed into.

---

<div class="post-metadata">

### Author: ![geoffw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://discourse.vtk.org/u/geoffw)
#### Post date: [March 20, 2019, 12:26am UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/3 "2019-03-20T00:26:14Z")

</div>

Andras,

I am using the non-native widget. That’s the one that doesn’t work. If I use QVTKOpenGLNativeWidget, since it inherits from QOpenGLWidget, the QT calls to initialize OpenGL are made (initializeGL). But if I use QVTKOpenGLWidget, the Qt OpenGL initialization never seems to occur. I have been poking through the source code and I can’t figure out how it is supposed to happen. QVTKOpenGLWidget has a QVTKOPenGLWIndow\* inside of it. This class has an initilalizeGL call but it is never called and there doesn’t seem to be any external way to call it.

thank you for any help.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [March 20, 2019, 1:50pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/4 "2019-03-20T13:50:28Z")

</div>

According to [QVTKOpenGLWidget documentation](https://vtk.org/doc/nightly/html/classQVTKOpenGLWidget.html), the non-native widget works only in special cases (the widget is not in a scrolling area, etc.).

QVTKOpenGLNativeWidget should work well (we use it extensively in 3D Slicer for very complex visualizations). You can try to tune anti-aliasing settings to improve appearance of lines. You may need to change the default surface format for the application if you want to use MSAA. We currently don’t use MSAA in 3D Slicer, so I cannot confirm if it works well, but there are VTK tests for that, so I assume that it does.

---

<div class="post-metadata">

### Author: ![geoffw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://discourse.vtk.org/u/geoffw)
#### Post date: [March 20, 2019, 3:06pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/5 "2019-03-20T15:06:06Z")

</div>

Andras,

Thank you for the advice. I believe the conditions we are using QVTKOpenGLWidget are appropriate, but still it doesn’t work with 8.2. I can get QVTLOpenGLNativeWidget to work, but certain visualizations being done via glsl fragment shader code don’t seem to work right. Simple stuff like drawing lines and shading volumes. I guess it may be easier to try and get QVTLOpenGLNativeWidget to work, But does anyone understand how QVTKOpenGLWidget is supposed to initialize OpenGL? It doesn’t inherit from qopenglwidget, so there must be a different mechanism for initialization, but I can’t find it…

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 20, 2019, 3:22pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/6 "2019-03-20T15:22:05Z")

</div>

Hello, and welcome to the VTK community.

There are multiples QVTKOpenGLWidget example usage in GUISupport/Qt/Testing/Cxx directory.  
This may help you understand how to use it.

---

<div class="post-metadata">

### Author: ![geoffw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://discourse.vtk.org/u/geoffw)
#### Post date: [March 20, 2019, 3:46pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/7 "2019-03-20T15:46:31Z")

</div>

Mathieu,

Thanks, I found the answer there:

```
// Make sure that the widget context is valid before making OpenGL calls.
// This should only take up to 4 calls to processEvents(). If this test keeps
// timing out, consider that the widget initialization is broken.
while (!_qvtkWidget->isValid()) {
    app.processEvents();
}

```

The OpenGL initialization occurs as a result of a Qt event! DOH!

---

<div class="post-metadata">

### Author: ![tchen87](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tchen87/32/1548_2.png) [@tchen87](https://discourse.vtk.org/u/tchen87)
#### Post date: [March 27, 2020, 8:27pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/8 "2020-03-27T20:27:34Z")

</div>

Hi,

I am running into this same issue, but haven’t been able to get proper OpenGL initialization to take place even when adding a block of code similar to the posted solution:

```auto
while (!_qvtkWidget->isValid()) {
    app.processEvents();
}

```

After doing a deep debug dive, it looks like `QVTKOpenGLWindow::initializeGL` is indeed getting called right before the first call to `vtkGenericOpenGLRenderWindow::Render`. But I am still seeing the long list of errors in cache state.

Any ideas on what could be going wrong or other things that need to happen for the widget to initialize properly?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 30, 2020, 2:21am UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/9 "2020-03-30T02:21:53Z")

</div>

Are you able to use the example I referenced ?

---

<div class="post-metadata">

### Author: ![tchen87](https://discourse.vtk.org/user_avatar/discourse.vtk.org/tchen87/32/1548_2.png) [@tchen87](https://discourse.vtk.org/u/tchen87)
#### Post date: [March 30, 2020, 10:46pm UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/10 "2020-03-30T22:46:04Z")

</div>

Yes - the example works OK without any issues. One thing to note is that I am working with a stereo based application. So the example perhaps does not take into account all of the necessary factors.

I have also noticed that rather than the entire list of errors that Geoff was seeing in his initial problem statement, I am only seeing errors in the cache state for

```auto
GL_SCISSOR_BOX

```

and

```auto
GL_VIEWPORT

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 31, 2020, 1:31am UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/11 "2020-03-31T01:31:15Z")

</div>

Please, open your own topic then, if the errors are different.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [March 31, 2020, 1:31am UTC](https://discourse.vtk.org/t/opengl-state-errors-with-qvtkopenglwidget-and-vtk8-2/539/12 "2020-03-31T01:31:17Z")

</div>


