<div dir="ltr">2016-05-23 16:55 GMT+02:00 Elvis Stansvik <span dir="ltr"><<a href="mailto:elvis.stansvik@orexplore.com" target="_blank">elvis.stansvik@orexplore.com</a>></span>:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><div class="h5">2016-05-23 16:28 GMT+02:00 Elvis Stansvik <span dir="ltr"><<a href="mailto:elvis.stansvik@orexplore.com" target="_blank">elvis.stansvik@orexplore.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div>I'm trying to use a context view (my goal is to show an XYChart) inside a QVTKRenderWindowInteractor, but I'm struggling to get the initialization of render window / interactor right.<br><br></div>The docs for vtkRenderViewBase (base class for vtkContextView) says:<br><br>"In order to use the view with a QVTKWidget the following code is required to ensure the interactor and render window are initialized properly.<br><br>QVTKWidget *widget = new QVTKWidget;<br>vtkContextView *view = vtkContextView::New();<br>view->SetInteractor(widget->GetInteractor());<br>widget->SetRenderWindow(view->GetRenderWindow());"<br><br></div>but this is about QVTKWidget, the C++ widget class, which is quite different from the Python QVTKRenderWindowInteractor class.<br><br></div>My failed attempt to recreate this initialization sequence when using QVTKRenderWindowInteractor is this:<br><br><br>class TestWidget(QVTKRenderWindowInteractor):<br><br> def __init__(self, parent=None):<br><br> # We need a render window and interactor to pass to<br> # QVTKRenderWindowInteractor constructor.<br> self.renderWindow = vtkRenderWindow()<br> self.interactor = vtkGenericRenderWindowInteractor()<br> self.interactor.SetRenderWindow(self.renderWindow)<br><br> super(TestWidget, self).__init__(<br> parent, rw=self.renderWindow, iren=self.interactor)<br><br> # Create a context view and set it to use the same<br> # interactor as the QVTKRenderWindowInteractor.<br> self.contextView = vtkContextView()<br> self.contextView.SetInteractor(self.interactor)<br><br> # Create chart and add it to the scene of the context view.<br> self.chart = vtkChartXY()<br> self.contextView.GetScene().AddItem(self.chart)<br><br> # Initialize and start.<br> self.Initialize()<br> self.Start()<br><br><br>if __name__ == '__main__':<br><br> app = QApplication(argv)<br><br> widget = TestWidget()<br> widget.show()<br><br> exit(app.exec_())<br><br><br></div>But something goes wrong here, because two windows show up (see attached screenshot). The application also segfaults on exit. Besides, it looks very kludgy to have to do stuff before the call to super() here. Is this really the right way to use the API?<br><br></div>If anyone has worked with QVTKRenderWindowInteractor and vtkContextView, I'd much appriciate some advice.<br></div></div></div></blockquote><div><br></div></div></div><div>Inspired by an older post on this subject [1], I was able to get it partly working with:<span class=""><br><br>class TestWidget(QVTKRenderWindowInteractor):<br><br> def __init__(self, parent=None):<br></span> super(TestWidget, self).__init__(parent)<br><br> self.contextView = vtkContextView()<br> self.chart = vtkChartXY()<br> self.contextView.GetScene().AddItem(self.chart)<br><br></div><div> # Add renderer of context view to the render window of the widget.<br></div><div> self.GetRenderWindow().AddRenderer(self.contextView.GetRenderer())<br><br> self.Initialize()<br> self.Start()<br><br>The chart is empty here, but I've verified by adding some items to it that it at least shows up correctly, in a single window.<br><br>The problem is that interaction of course doesn't work, and if I also add<br><br></div><div> self.contextView.SetInteractor(self._Iren)<br><br></div><div>to set the interactor of the context view to the vtkGenericRenderWindowInteractor backing the QVTKRenderWindowInteractor, I'm back to the problem with two windows showing up.<br><br></div><div>I need interaction to work, because my goal is to show an opacity function editor item in the chart.<br><br></div><div>Thankful for any and all advice!<br></div></div></div></div></blockquote><div><br></div><div>To better illustrate, here's the scatter plot Python example from the wiki, with the code I've added to try to make it work with QVTKRenderWindowInteractor.<br><br></div><div>The result is that when the application is executed, a "Visualization Toolkit - OpenGL" window first shows up, where interaction works fine. This is not the Qt window though. The Qt window shows up if I close the first window, and interaction does not work (try hovering the data points).<br><br></div><div>How would this example need to be modified for it to work? (chart shown in the Qt window, with interaction working).<br><br></div><div>Best regards,<br></div><div>Elvis<br></div><div><br># <ADDED><br>app = QApplication(sys.argv)<br><br>widget = QVTKRenderWindowInteractor()<br>widget.Initialize()<br>widget.Start()<br># </ADDED><br><br>view = vtk.vtkContextView()<br>view.GetRenderer().SetBackground(1.0, 1.0, 1.0)<br>view.GetRenderWindow().SetSize(400, 300)<br><br>chart = vtk.vtkChartXY()<br>view.GetScene().AddItem(chart)<br>chart.SetShowLegend(True)<br><br>table = vtk.vtkTable()<br><br>arrX = vtk.vtkFloatArray()<br>arrX.SetName('X Axis')<br><br>arrC = vtk.vtkFloatArray()<br>arrC.SetName('Cosine')<br><br>arrS = vtk.vtkFloatArray()<br>arrS.SetName('Sine')<br><br>arrT = vtk.vtkFloatArray()<br>arrT.SetName('Sine-Cosine')<br><br>table.AddColumn(arrC)<br>table.AddColumn(arrS)<br>table.AddColumn(arrX)<br>table.AddColumn(arrT)<br><br>numPoints = 40<br><br>inc = 7.5 / (numPoints - 1)<br>table.SetNumberOfRows(numPoints)<br>for i in range(numPoints):<br> table.SetValue(i, 0, i * inc)<br> table.SetValue(i, 1, math.cos(i * inc))<br> table.SetValue(i, 2, math.sin(i * inc))<br> table.SetValue(i, 3, math.sin(i * inc) - math.cos(i * inc))<br><br>points = chart.AddPlot(vtk.vtkChart.POINTS)<br>points.SetInputData(table, 0, 1)<br>points.SetColor(0, 0, 0, 255)<br>points.SetWidth(1.0)<br>points.SetMarkerStyle(vtk.vtkPlotPoints.CROSS)<br><br>points = chart.AddPlot(vtk.vtkChart.POINTS)<br>points.SetInputData(table, 0, 2)<br>points.SetColor(0, 0, 0, 255)<br>points.SetWidth(1.0)<br>points.SetMarkerStyle(vtk.vtkPlotPoints.PLUS)<br><br>points = chart.AddPlot(vtk.vtkChart.POINTS)<br>points.SetInputData(table, 0, 3)<br>points.SetColor(0, 0, 255, 255)<br>points.SetWidth(1.0)<br>points.SetMarkerStyle(vtk.vtkPlotPoints.CIRCLE)<br><br>view.GetRenderWindow().SetMultiSamples(0)<br>view.GetInteractor().Initialize()<br>view.GetInteractor().Start()<br><br># <ADDED><br>widget.GetRenderWindow().AddRenderer(view.GetRenderer())<br>widget.setWindowTitle('Qt Widget')<br>widget.show()<br><br>exit(app.exec_())<br># </ADDED><br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div><br></div><div>Elvis<br><br></div><div>[1] <a href="http://www.vtk.org/pipermail/vtkusers/2011-July/068893.html" target="_blank">http://www.vtk.org/pipermail/vtkusers/2011-July/068893.html</a><br> <br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><br></div>Cheers,<br></div>Elvis<br><br></div>
</blockquote></div><br></div></div>
</blockquote></div><br></div></div>