# Showing a qMRRMLWidget instead of the view layout

**URL:** https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178
**Category:** Development
**Tags:** gui
**Created:** [February 24, 2021, 1:17pm UTC](https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178 "2021-02-24T13:17:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![pll\_llq](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pll_llq/32/9408_2.png) [@pll\_llq](https://discourse.slicer.org/u/pll_llq)
#### Post date: [February 24, 2021, 1:17pm UTC](https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178/1 "2021-02-24T13:17:34Z")

</div>

Hi 👋

I want to display a widget that will takes the space used by the view layout.  
As I understand after reading [reply to this post](https://discourse.slicer.org/t/how-to-include-qwidgets-in-slicer-qml-layout/13259) I can’t have a QMRMLWidget as a part of a custom layout.

I tried to manually set my widget to be the `centralWidget` of the `mainWindow` with

```python
mainWindow = slicer.util.mainWindow()
mainWindow.setCentralWidget(mywidget)

```

and I see the result, but the existing layout manager get’s destroyed.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/3/1/313fe76ec4bbc9b16b809ee98859d2a407846e9a.png)

So when I try to revert to a view layout I am met with an error:

```plaintext
ValueError: Trying to call 'setLayout' on a destroyed qSlicerLayoutManager object

```

[Here’s the sample code](https://gist.github.com/piiq/62df9c295d4467c2712760dc0ab0747e). Copy-paste into Slicer python interpreter works.

I guess that setting the centralWidget like that is not a good idea. Can anyone advice how to correctly show my widget in the middle of the main window?

---

<div class="post-metadata">

### Author: ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)
#### Post date: [February 24, 2021, 1:37pm UTC](https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178/2 "2021-02-24T13:37:48Z")

</div>

Custom widgets are placed inside the view layout. The simplest way is to use `qSlicerSingletonViewFactory` as it is done in the DICOM module.

---

<div class="post-metadata">

### Author: ![pll\_llq](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pll_llq/32/9408_2.png) [@pll\_llq](https://discourse.slicer.org/u/pll_llq)
#### Post date: [February 24, 2021, 1:39pm UTC](https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178/3 "2021-02-24T13:39:57Z")

</div>

Thanks, this something that I wanted to try.

---

<div class="post-metadata">

### Author: ![pll\_llq](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pll_llq/32/9408_2.png) [@pll\_llq](https://discourse.slicer.org/u/pll_llq)
#### Post date: [February 24, 2021, 4:24pm UTC](https://discourse.slicer.org/t/showing-a-qmrrmlwidget-instead-of-the-view-layout/16178/4 "2021-02-24T16:24:43Z")

</div>

Thanks, it worked.

I’ve added

```auto
viewFactory = slicer.qSlicerSingletonViewFactory()
viewFactory.setWidget(mywidget)
viewFactory.setTagName("helloLayout")
layoutManager.registerViewFactory(viewFactory)

layout = (
    "<layout type=\"horizontal\">"
    " <item>"
    " <helloLayout></helloLayout>"
    " </item>"
    "</layout>"
)

customLayoutId = 42

layoutNode = slicer.app.layoutManager().layoutLogic().GetLayoutNode()
layoutNode.AddLayoutDescription(customLayoutId, layout)

layoutManager.setLayout(customLayoutId)

```

instead of manipulating the centralWidget and got a nice layout with my widget.
