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

**URL:** https://discourse.vtk.org/t/embedding-vtk-render-in-a-dialog-box-in-pyqt-i-e-not-using-qmainwindow/5492
**Category:** Support
**Created:** [April 4, 2021, 10:31am UTC](https://discourse.vtk.org/t/embedding-vtk-render-in-a-dialog-box-in-pyqt-i-e-not-using-qmainwindow/5492 "2021-04-04T10:31:11Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Vardonir](https://discourse.vtk.org/user_avatar/discourse.vtk.org/vardonir/32/2924_2.png) [@Vardonir](https://discourse.vtk.org/u/Vardonir)
#### Post date: [April 4, 2021, 10:31am UTC](https://discourse.vtk.org/t/embedding-vtk-render-in-a-dialog-box-in-pyqt-i-e-not-using-qmainwindow/5492/1 "2021-04-04T10:31:11Z")

</div>

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.

---

<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: [April 6, 2021, 1:34pm UTC](https://discourse.vtk.org/t/embedding-vtk-render-in-a-dialog-box-in-pyqt-i-e-not-using-qmainwindow/5492/2 "2021-04-06T13:34:12Z")

</div>

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:

```auto
class TestVTKinQFrame(QFrame):

```

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

```auto
self.frame.setLayout(self.vl)

```

Why not `self.setLayout(self.vl)`?

---

<div class="post-metadata">

### Author: ![Edoardo\_Pasca](https://discourse.vtk.org/user_avatar/discourse.vtk.org/edoardo_pasca/32/742_2.png) [@Edoardo\_Pasca](https://discourse.vtk.org/u/Edoardo_Pasca)
#### Post date: [April 9, 2021, 6:56am UTC](https://discourse.vtk.org/t/embedding-vtk-render-in-a-dialog-box-in-pyqt-i-e-not-using-qmainwindow/5492/3 "2021-04-09T06:56:33Z")

</div>

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](https://github.com/vais-ral/CILViewer/blob/master/Wrappers/Python/ccpi/viewer/QCILViewerWidget.py#L10): 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
