# Layout Manager cannot get the right qMRMLSliceWidget

**URL:** https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460
**Category:** Development
**Created:** [May 29, 2024, 10:37am UTC](https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460 "2024-05-29T10:37:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)
#### Post date: [May 29, 2024, 10:37am UTC](https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460/1 "2024-05-29T10:37:33Z")

</div>

I create a qMRMLSliceWidget by using this [scipt](https://slicer.readthedocs.io/en/latest/developer_guide/script_repository.html#show-a-slice-view-outside-the-view-layout).

```auto
>>> # layout name is used to create and identify the underlying slice node and should be set to a value that is not used in any of the layouts owned by the layout manager
>>> layoutName = "TestSlice1"
>>> layoutLabel = "TS1"
>>> layoutColor = [1.0, 1.0, 0.0]
>>> # ownerNode manages this view instead of the layout manager (it can be any node in the scene)
>>> viewOwnerNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLScriptedModuleNode")
>>> 
>>> # Create MRML nodes
>>> viewLogic = slicer.vtkMRMLSliceLogic()
>>> viewLogic.SetMRMLScene(slicer.mrmlScene)
>>> viewNode = viewLogic.AddSliceNode(layoutName)
>>> viewNode.SetLayoutLabel(layoutLabel)
>>> viewNode.SetLayoutColor(layoutColor)
>>> viewNode.SetAndObserveParentLayoutNodeID(viewOwnerNode.GetID())
True
>>> 
>>> # Create widget
>>> viewWidget = slicer.qMRMLSliceWidget()
>>> viewWidget.setMRMLScene(slicer.mrmlScene)
>>> viewWidget.setMRMLSliceNode(viewNode)
>>> sliceLogics = slicer.app.applicationLogic().GetSliceLogics()
>>> viewWidget.setSliceLogics(sliceLogics)
>>> sliceLogics.AddItem(viewWidget.sliceLogic())

```

Then I use the layoutManager to get the slice widget I have just created, but layoutManager give me a wrong object:

```auto
>>> print(viewWidget)
qMRMLSliceWidget(0x560a750d2900, name="qMRMLSliceWidget") 
>>> print(layoutManager.sliceWidget("TestSlice1"))
qMRMLSliceWidget(0x560a742ca520, name="qMRMLSliceWidgetTestSlice1") 

```

How does this happen?

---

<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: [May 29, 2024, 1:45pm UTC](https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460/2 "2024-05-29T13:45:57Z")

</div>

The layout manager has no way of knowing that you created some view widgets.

Also note that since you have explicitly set that the layout manager is not the owner of the view node (by calling `SetAndObserveParentLayoutNodeID`), the layout manager should ignore this view node completely. The layout manager only knows about the view node because you have set the parent layout node too late (after it was already added to the scene).

---

<div class="post-metadata">

### Author: ![nnzzll](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/nnzzll/32/16350_2.png) [@nnzzll](https://discourse.slicer.org/u/nnzzll)
#### Post date: [May 30, 2024, 2:42am UTC](https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460/3 "2024-05-30T02:42:14Z")

</div>

My final purpose is to use SegmentEditor on my own widget, if layout manager has no way of knowing newly created widgets, then I should use the default slice widget(“Red”,“Green”,etc) right?

I tried the code below but it didn’t work

```auto
>>> layoutManager = slicer.app.layoutManager()
>>> layoutNode = layoutManager.layoutLogic().GetLayoutNode()
>>> myWidget = qt.QWidget()
>>> viewFactory = slicer.qSlicerSingletonViewFactory()
>>> viewFactory.setTagName("MyWidget")
>>> viewFactory.setWidget(myWidget)
>>> layoutManager.registerViewFactory(viewFactory)
>>> viewLayout = """<layout type="vertical"><item><MyWidget></MyWidget></item></layout>"""
>>> layoutNode.AddLayoutDescription(501, viewLayout)
True
>>> layoutManager.setLayout(501)
>>> gridLayout = qt.QGridLayout()
>>> sliceWidget = layoutManager.sliceWidget("Red")
>>> gridLayout.addWidget(sliceWidget)
>>> myWidget.setLayout(gridLayout)
>>> print(myWidget)
QWidget(0x56546c7cf570) 
>>> print(sliceWidget.parent())
QWidget(0x56546c7cf570) 
>>> print(sliceWidget.isVisible())
False
>>> 

```

Now the slice widget Red is child widget of `myWidget`, but it is unvisible. Is there anything wrong with this code?

---

<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: [May 30, 2024, 3:29am UTC](https://discourse.slicer.org/t/layout-manager-cannot-get-the-right-qmrmlslicewidget/36460/4 "2024-05-30T03:29:05Z")

</div>

The segment editor widget queries the list of views from the layout manager, so if you want to edit segments then you have to let the layout manager create the view and widget for you. If want to have an additional view outside the main viewport then you can specify multiple viewports (see examples in the script repository).
