Layout Manager cannot get the right qMRMLSliceWidget

I create a qMRMLSliceWidget by using this scipt.

>>> # 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:

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

How does this happen?

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).

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

>>> 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?

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).