# Error occur when placing two QVTKRenderWindowInteractor with vtkContextView in a single window

**URL:** https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369
**Category:** Development
**Tags:** python
**Created:** [September 27, 2023, 2:43am UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369 "2023-09-27T02:43:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![MG.Hu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MG.Hu](https://discourse.vtk.org/u/MG.Hu)
#### Post date: [September 27, 2023, 2:43am UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/1 "2023-09-27T02:43:22Z")

</div>

When I add two separate interactive vtkChartXY into a single window with PyQt5 and VTK. The window appears and works correctly. However, when it is closed there are several errors.

vtkWin32OpenGLRenderWin:758 ERR| vtkWin32OpenGLRenderWindow (00000254D385E180): failed to get valid pixel format. vtkOpenGLRenderWindow.c:511 ERR| vtkWin32OpenGLRenderWindow (00000254D385E180): GLEW could not be initialized: Missing GL version vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined. vtkOpenGLState.cxx:1795 WARN| Hardware does not support the number of textures defined.

The errors are similar to the post [wglMakeCurrent failed in MakeCurrent after closed a window with two vtk widget.](https://discourse.vtk.org/t/wglmakecurrent-failed-in-makecurrent-after-closed-a-window-with-two-vtk-widget/5899), however, the problem still exists when there is vtkContextView.

Here is a demo:

```
from vtk import *
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import QtWidgets

class ChartWidget(QtWidgets.QWidget):
    def __init__ (self):
        super(). __init__ ()

        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)

        self.widget = QVTKRenderWindowInteractor(self)
        layout.addWidget(self.widget)

        chart = vtkChartXY()
        self.view = vtkContextView()
        self.view.SetRenderWindow(self.widget.GetRenderWindow())
        self.view.GetScene().AddItem(chart)

        # x-y plot
        f = vtkPiecewiseFunction()
        f.AddPoint(0, 0.0)
        f.AddPoint(500, 0.5)
        pf = vtkPiecewiseFunctionItem()
        pf.SetPiecewiseFunction(f)
        chart.AddPlot(pf)

        self.view.GetInteractor().Initialize()
        self.view.GetInteractor().Start()

    def closeEvent(self, event):
        super().closeEvent(event)
        self.widget.Finalize()

class MainWindow(QtWidgets.QWidget):

    def __init__ (self):
        super(). __init__ ()

        layout = QtWidgets.QHBoxLayout()
        self.setLayout(layout)

        self.w1 = ChartWidget()
        self.w2 = ChartWidget()
        layout.addWidget(self.w1)
        layout.addWidget(self.w2)

    def closeEvent(self, event):
        super().closeEvent(event)
        self.w1.close()
        self.w2.close()

if __name__ == " __main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()

```

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [October 1, 2023, 9:17pm UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/2 "2023-10-01T21:17:07Z")

</div>

Hello,

The usual protocol in GUI programming is to forward events to the base class last, not first. For example, try changing this:

> [@MG.Hu](#):
>
> ```auto
> def closeEvent(self, event):
> super().closeEvent(event)
> self.widget.Finalize()
> 
> ```

to this:

> [@MG.Hu](#):
>
> ```auto
> def closeEvent(self, event):
> self.widget.Finalize()
> super().closeEvent(event)
> 
> ```

The same rationale applies to the other event-handling code in your program.

take care,

Paulo

---

<div class="post-metadata">

### Author: ![MG.Hu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MG.Hu](https://discourse.vtk.org/u/MG.Hu)
#### Post date: [October 9, 2023, 7:41am UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/3 "2023-10-09T07:41:28Z")

</div>

Thanks Paulo! The errors seems still exist after the modification in the demo.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [October 9, 2023, 9:45pm UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/4 "2023-10-09T21:45:56Z")

</div>

Hello,

Did you apply the expected event forwarding protocol to all your event handling code?

best,

PC

---

<div class="post-metadata">

### Author: ![MG.Hu](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/b5a626/32.png) [@MG.Hu](https://discourse.vtk.org/u/MG.Hu)
#### Post date: [October 10, 2023, 8:19am UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/6 "2023-10-10T08:19:42Z")

</div>

Actually, if only one widget was added in the demo codes, no error appears after closing the application window. But when two widgets (w1 and w2) were added, there are errors when closing.

Here is the modified code:

```auto
from vtk import *
from vtkmodules.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import QtWidgets

class ChartWidget(QtWidgets.QWidget):
    def __init__ (self):
        super(). __init__ ()

        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)

        self.widget = QVTKRenderWindowInteractor(self)
        layout.addWidget(self.widget)

        chart = vtkChartXY()
        self.view = vtkContextView()
        self.view.SetRenderWindow(self.widget.GetRenderWindow())
        self.view.GetScene().AddItem(chart)

        # x-y plot
        f = vtkPiecewiseFunction()
        f.AddPoint(0, 0.0)
        f.AddPoint(500, 0.5)
        pf = vtkPiecewiseFunctionItem()
        pf.SetPiecewiseFunction(f)
        chart.AddPlot(pf)

        self.view.GetInteractor().Initialize()
        self.view.GetInteractor().Start()

class MainWindow(QtWidgets.QWidget):

    def __init__ (self):
        super(). __init__ ()

        layout = QtWidgets.QHBoxLayout()
        self.setLayout(layout)

        self.w1 = ChartWidget()
        layout.addWidget(self.w1)
        
        self.w2 = ChartWidget() # no error if these two lines were commented out.
        layout.addWidget(self.w2) # no error if these two lines were commented out.

if __name__ == " __main__":
    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    app.exec_()

```

---

<div class="post-metadata">

### Author: ![xiang](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/x/9fc348/32.png) [@xiang](https://discourse.vtk.org/u/xiang)
#### Post date: [March 25, 2025, 2:07pm UTC](https://discourse.vtk.org/t/error-occur-when-placing-two-qvtkrenderwindowinteractor-with-vtkcontextview-in-a-single-window/12369/7 "2025-03-25T14:07:04Z")

</div>

I meet the same question ,May I ask if you have solved this problem yet?
