From 4d9b9417e25d3682a5b03052cdb72a605a399760 Mon Sep 17 00:00:00 2001 From: Ruben de Bruin Date: Thu, 20 Jun 2019 15:24:40 +0200 Subject: [PATCH] isse #33 : integrated Qt into plotter creation such that icons (axis) can be created by show() method --- examples/basic/qt_window.py | 34 ++++++++++++++++++++++++++++++++++ vtkplotter/plotter.py | 26 +++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 examples/basic/qt_window.py diff --git a/examples/basic/qt_window.py b/examples/basic/qt_window.py new file mode 100644 index 00000000..fd57c09d --- /dev/null +++ b/examples/basic/qt_window.py @@ -0,0 +1,34 @@ +import sys +from PyQt5 import Qt +from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor + +from vtkplotter import Plotter, Cone + +# settings.usingQt = True <-- not needed anymore, automatically triggered by passing a qtWidget to Plotter + + +class MainWindow(Qt.QMainWindow): + def __init__(self, parent=None): + + Qt.QMainWindow.__init__(self, parent) + self.frame = Qt.QFrame() + self.vl = Qt.QVBoxLayout() + self.vtkWidget = QVTKRenderWindowInteractor(self.frame) + self.vl.addWidget(self.vtkWidget) + + vp = Plotter(qtWidget=self.vtkWidget, axes=4, verbose=False) + + vp += Cone() + vp.show() # to create renderer and add the actors + + # set-up the rest of the Qt window + self.frame.setLayout(self.vl) + self.setCentralWidget(self.frame) + + self.show() # <--- show the Qt Window + + +if __name__ == "__main__": + app = Qt.QApplication(sys.argv) + window = MainWindow() + app.exec_() \ No newline at end of file diff --git a/vtkplotter/plotter.py b/vtkplotter/plotter.py index 25505dd6..c065d925 100644 --- a/vtkplotter/plotter.py +++ b/vtkplotter/plotter.py @@ -299,7 +299,7 @@ class Plotter: :param list shape: shape of the grid of renderers in format (rows, columns). Ignored if N is specified. :param int N: number of desired renderers arranged in a grid automatically. - :param list pos: (x,y) position in pixels of top-left corneer of the rendering window + :param list pos: (x,y) position in pixels of top-left corner of the rendering window on the screen :param size: size of the rendering window. If 'auto', guess it based on screensize. :param screensize: physical size of the monitor screen @@ -365,6 +365,13 @@ class Plotter: :param bool interactive: if True will stop after show() to allow interaction w/ window :param bool offscreen: if True will not show the rendering window :param bool depthpeeling: depth-peel volumes along with the translucent geometry + :param QVTKRenderWindowInteractor qtWidget: + + render in a Qt-Widget using an QVTKRenderWindowInteractor. + Overrides offscreen to True + Overides interactive to False + Sets setting.usingQt to True + See Also: example qt_windows.py |multiwindows| """ @@ -386,11 +393,18 @@ def __init__( interactive=None, offscreen=False, depthpeeling=False, + qtWidget = None ): settings.plotter_instance = self settings.plotter_instances.append(self) + if qtWidget is not None: + # overrides the interactive and offscreen properties + interactive = False + offscreen = True + settings.usingQt = True + if interactive is None: if N or shape != (1, 1): interactive = False @@ -421,6 +435,7 @@ def __init__( self.backgrcol = bg self.offscreen = offscreen self.showFrame = True + self.qtWidget = qtWidget # (QVTKRenderWindowInteractor) # mostly internal stuff: self.justremoved = None @@ -574,6 +589,11 @@ def __init__( for r in self.renderers: self.window.AddRenderer(r) + if self.qtWidget is not None: + self.interactor = self.qtWidget.GetRenderWindow().GetInteractor() + self.window.SetOffScreenRendering(True) + return + if offscreen: self.window.SetOffScreenRendering(True) self.interactive = False @@ -1335,6 +1355,9 @@ def scan(wannabeacts): colors.printc("~times Error in show(): wrong renderer index", at, c=1) return + if self.qtWidget is not None: + self.qtWidget.GetRenderWindow().AddRenderer(self.renderer) + if not self.camera: if isinstance(camera, vtk.vtkCamera): self.camera = camera @@ -1373,6 +1396,7 @@ def scan(wannabeacts): if hasattr(c, 'renderedAt'): c.renderedAt.discard(at) + if self.axes is not None and not settings.notebookBackend: addons.addAxes()