Embedding VTK render in a dialog box in PyQT (i.e., not using QMainWindow)

I have a class that’s subclassed from QFrame that looks like this

class TestVTKinQFrame(QFrame):
    def __init__(self):
        super(TestVTKinQFrame, self).__init__()

        self.vl = QVBoxLayout()
        self.vtkWidget = QVTKRenderWindowInteractor(self)
        self.vl.addWidget(self.vtkWidget)

        self.ren = vtk.vtkRenderer()
        self.vtkWidget.GetRenderWindow().AddRenderer(self.ren)
        self.iren = self.vtkWidget.GetRenderWindow().GetInteractor()

        # Create source
        source = vtk.vtkSphereSource()
        source.SetCenter(0, 0, 0)
        source.SetRadius(5.0)

        # Create a mapper
        mapper = vtk.vtkPolyDataMapper()
        mapper.SetInputConnection(source.GetOutputPort())

        # Create an actor
        actor = vtk.vtkActor()
        actor.SetMapper(mapper)

        self.ren.AddActor(actor)

        self.ren.ResetCamera()

        self.frame.setLayout(self.vl)
        # self.setCentralWidget(self.frame)

        self.show()
        self.iren.Initialize()

And I have a widget that uses this frame

class Widget(QWidget):
    def __init__(self):
        super().__init__()
        # self.model = input_model
        self.model = TestVTKinQFrame()

        self._top_layout = QHBoxLayout(self)
        self._top_layout.addWidget(self.model, stretch=85)
        self.setLayout(self._top_layout)

where this widget is added to another layout in a QDialog.

But it keeps crashing for some reason.

If I define it as a class (that’s not subclassed from anything), the VTK render work, but it ends up being very small. Putting addWidget(stretch=90) does nothing for the size.

Can you provide a complete (but small) program that demonstrates the problem?

One thing about your class doesn’t make sense. It’s a frame class:

class TestVTKinQFrame(QFrame):

But it seems to have “frame” as a member, which seems odd:

self.frame.setLayout(self.vl)

Why not self.setLayout(self.vl)?

Hi

in my experience Qt crashes if something is not defined. In your snippet, it seems self.frame isn’t defined, but you try to dereference it, hence Qt crashes.

Also, as @dgobbi points out, your TestVTKinQFrame is a QFrame widget that you can put in other Qt widgets or windows.

I have a similar setting: my VTK render window is inside a QFrame. I can then use that QFrame inside other Qt Widgets, like a QDockWidget or in a layout.

Edo