Cannot use QVTKRenderWindowInteractor and QWebEngineView together in one application.

When I use QVTKRenderWindowInteractor and QWebEngineView together in my PyQt source running under Windows 10, I get the following error whenever I close the application window.

vtkWin32OpenGLRenderWin:217 ERR| vtkWin32OpenGLRenderWindow (000001EA17F351C0): wglMakeCurrent failed in MakeCurrent(), error: The handle is invalid.

I managed to solve it by adding the following in my code

def closeEvent(self, event):
    self.vtkWidget.Finalize() # This will also work --> self.vtkWidget.close()

However, I am left with a problem where I am unable to interact with my QWebEngineView in a specific scenario. Please note that this happens only when I have vtkRenderer present in my application.
In my application UI, I have a slider and two QFrames with QVTKRenderWindowInteractor and QWebEngineView instances.

Issue Reproduction Steps

  1. Run Application using provided py script. (you will see a working empty vtkRenderer and a webview showing a page)
  2. Click on vtkRenderer window.
  3. Try interacting with webview, but you will see that you cannot.
  4. To interact with webview again, do interaction with slider or resize the window and come back to webview.

I have attached an easily runnable code for you to check and comment. (1.9 KB)

Any help on this is greatly appreciated.

In 3D Slicer, instead of using Python as the application, we embed Python in a Qt application (so that the Qt application sets up OpenGL, manages event queue, threading, etc. as usual). In this configuration everything works well - we can have web views and VTK renderers (and interactive Python console, Jupyter kernel, virtual reality views, real-time image transfer via network, etc.) all running in parallel, without blocking each other. Embedding Python may be an overkill if you just want to create a small Python script but if you want to develop a standalone application anyway then it may be a good option.

Unfortunately, I cannot switch to C++ now.
Can I hope solving this by implementing multithreading in python?
One thread handling vtkrenderer and another handling webView?
Any other alternate solutions?

You can keep everything in Python, you just need to run your script in any Qt application that embeds Python (and allows running custom Python code). You can use existing applications, such as 3D Slicer or Paraview, or create a minimal application yourself.

There is no real multi-threading in Python (execution is still sequential), unless you manipulate the Python GIL. But it is more likely that you don’t need multiple threads just pump all the event loop of both the VTK renderer and the web view. I don’t know any details, these are just potential approaches for you to explore.

1 Like