Showing a qMRRMLWidget instead of the view layout

Hi :wave:

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

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

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

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

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

Here’s the sample code. 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?

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

1 Like

Thanks, this something that I wanted to try.

Thanks, it worked.

I’ve added

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.

1 Like