# Problem with VTK Chart interaction with PyQt

**URL:** https://discourse.vtk.org/t/problem-with-vtk-chart-interaction-with-pyqt/6561
**Category:** Support
**Created:** [September 6, 2021, 9:13am UTC](https://discourse.vtk.org/t/problem-with-vtk-chart-interaction-with-pyqt/6561 "2021-09-06T09:13:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hhebb](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hhebb/32/3902_2.png) [@hhebb](https://discourse.vtk.org/u/hhebb)
#### Post date: [September 6, 2021, 9:13am UTC](https://discourse.vtk.org/t/problem-with-vtk-chart-interaction-with-pyqt/6561/1 "2021-09-06T09:13:48Z")

</div>

I’m trying to Draw `VTK ChartXY` using `QVTKRenderWindowInteractor` because my program is designed to use `PyQt`. Problem is, showing chart is good but, i `can't interact` with the chart for example, zoom, pan, hovering. When i tested identical chart show task without QVTKRenderWindowInteractor, then interact event was perfect.

Here is my reference Code.

```auto
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5.QtWidgets import QVBoxLayout, QWidget, QApplication, QPushButton
import matplotlib.pyplot as plt
import numpy as np
import vtk
import math
import sys
import os

app = QApplication(sys.argv)

widget = QVTKRenderWindowInteractor()
widget.Initialize()
widget.Start()

view = vtk.vtkContextView()
view.GetRenderer().SetBackground(1.0, 1.0, 1.0)
view.GetRenderWindow().SetSize(400, 300)

chart = vtk.vtkChartXY()
view.GetScene().AddItem(chart)
chart.SetShowLegend(True)

table = vtk.vtkTable()

arrX = vtk.vtkFloatArray()
arrX.SetName('X Axis')

arrC = vtk.vtkFloatArray()
arrC.SetName('Cosine')

arrS = vtk.vtkFloatArray()
arrS.SetName('Sine')

arrT = vtk.vtkFloatArray()
arrT.SetName('Sine-Cosine')

table.AddColumn(arrC)
table.AddColumn(arrS)
table.AddColumn(arrX)
table.AddColumn(arrT)

numPoints = 40

inc = 7.5 / (numPoints - 1)
table.SetNumberOfRows(numPoints)
for i in range(numPoints):
    table.SetValue(i, 0, i * inc)
    table.SetValue(i, 1, math.cos(i * inc))
    table.SetValue(i, 2, math.sin(i * inc))
    table.SetValue(i, 3, math.sin(i * inc) - math.cos(i * inc))

points = chart.AddPlot(vtk.vtkChart.POINTS)
points.SetInputData(table, 0, 1)
points.SetColor(0, 0, 0, 255)
points.SetWidth(1.0)
points.SetMarkerStyle(vtk.vtkPlotPoints.CROSS)

view.GetRenderWindow().SetMultiSamples(0)
view.GetInteractor().Initialize()
view.GetInteractor().Start()

widget.GetRenderWindow().AddRenderer(view.GetRenderer())
widget.setWindowTitle('Qt Widget')
widget.show()

exit(app.exec_())

```

This example show `Pure VTK window` and after closing VTK window show `QtWidget window`. The Pure VTK window interact perfectly with my commant, QtWidget interact not at all.

Is anybody know how to make VTK chart interact well with PyQt system?? Thanks.

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [September 6, 2021, 1:48pm UTC](https://discourse.vtk.org/t/problem-with-vtk-chart-interaction-with-pyqt/6561/2 "2021-09-06T13:48:48Z")

</div>

The problem is that the window and interactor that are created by the view do not know about Qt, so they cannot handle Qt events.

But the solution is straightforward: tell the view to use the widget’s render window and interactor, like this:

```python
view.SetRenderWindow(widget.GetRenderWindow())

```

And remove these lines:

```python
widget.GetRenderWindow().AddRenderer(view.GetRenderer())

```

And also remove these lines:

```python
view.GetInteractor().Initialize()
view.GetInteractor().Start()

```

And, finally, move these lines to the the end, after `widget.show()`:

```auto
widget.Initialize()
widget.Start()

```

---

<div class="post-metadata">

### Author: ![hhebb](https://discourse.vtk.org/user_avatar/discourse.vtk.org/hhebb/32/3902_2.png) [@hhebb](https://discourse.vtk.org/u/hhebb)
#### Post date: [September 8, 2021, 4:53am UTC](https://discourse.vtk.org/t/problem-with-vtk-chart-interaction-with-pyqt/6561/3 "2021-09-08T04:53:04Z")

</div>

Thanks!
