Set Main Window title to name of opened MRB file

Hi all,

I am generating scenes containing patient imaging/segmentation data, saving as MRB, and sending to our clinical team. They often have several Slicer instances opened with different MRB files loaded.

Is it possible to set each Slicer instance Main Window title to the name of the opened MRB file?

Thank you!

Hi,

Two lines of python code (paste into python console for a test) can set the title of the main window:

mainWindow = slicer.util.mainWindow()
mainWindow.setWindowTitle("Your title")
1 Like

You can use slicer.mrmlScene.GetURL() to add the mrb file name to the window title.

However, since scene files are always imported (current scene content is not replaced) the mrb file name is not always an accurate description of the scene content. It might be safer to rely on what is shown in Data module or in corner annotations:

image

1 Like

@rbumm thank you for the code snippet. I combined this with Andras’s suggestion in the .slicerrc.py file. The outcome is exactly what I was looking for.

import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onNodeAdded(caller, event, calldata):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.NodeAddedEvent, onNodeAdded)

1 Like

Thanks for sharing your solution. Updating the window title is a very lightweight operation, so it should not matter that you do it whenever a node is added to the scene. If you just want to do something when a scene is loaded (e.g., because it is a more lengthy operation) then instead of slicer.vtkMRMLScene.NodeAddedEvent you can observe the slicer.vtkMRMLScene.EndImportEvent event instead.

I’ve made the following correction in the .slicerrc.py file, using slicer.vtkMRMLScene.EndImportEvent instead:

import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEventImportEnd(caller, event):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndImportEvent, onEventImportEnd)

For completeness, how should I handle the scene close event. Currently, the Main Window title remains when the scene is closed. I would like the title to return back to default when the scene is closed. I initially was going to use slicer.vtkMRMLScene.NodeRemovedEvent but sense this is not the best option

You can observe the slicer.vtkMRMLScene.EndCloseEvent to get notified about scene close.

1 Like

This is the final solution:

import os

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEventImportEnd(caller, event):
	windowTitle = os.path.basename(slicer.mrmlScene.GetURL())
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle(windowTitle)

@vtk.calldata_type(vtk.VTK_OBJECT)
def onEndClose(caller, event):
	mainWindow = slicer.util.mainWindow()
	mainWindow.setWindowTitle('Slicer')

slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndImportEvent, onEventImportEnd)
slicer.mrmlScene.AddObserver(slicer.vtkMRMLScene.EndCloseEvent, onEndClose)
3 Likes