How to add shortcut to delete item in subject hierarchy

Hi, may I know how to add shortcut to delete highlighted item in subject hierarchy? Right now, I can only delete it by selecting the item, and then right-click delete.

I think you’ll need to add a keyboard shortcut to the DeleteAction in qMRMLSubjectHierarchyTreeView.

Thanks. The code below works when I press ‘Del’ in the ‘Data’ modules.

def deleteSelectedSubjectHierarchyItem():
    dataWidget = slicer.modules.data.widgetRepresentation()
    shTreeView = slicer.util.findChild(dataWidget, name='SubjectHierarchyTreeView')
    shTreeView.deleteSelectedItems()

# Set up the shortcut
shortcut = qt.QShortcut(slicer.util.mainWindow())
shortcut.setKey(qt.QKeySequence("Del"))
shortcut.connect('activated()', deleteSelectedSubjectHierarchyItem)

What may be problematic is that you added the shortcut on the main window, so even if you don’t see the SH tree but press the button, the node will be deleted, and you’ll have no idea. I’d try to put it in the Slicer code itself, in the tree view class. It is a very small change, could you give it a try?

Do you mean to write these code to the c++ ( Slicer/Slicer: Multi-platform, free open source software for visualization and image computing.)? I am a beginner in 3D Slicer, so I do not know how to do it. May I ask for more details on how to implement this?

It is not much more code than what you wrote, just add the shortcut for the tree view itself (this).

Update: here is the contribution guide. If you want your name to appear among the Slicer developers and you have some time and motivation, this could be a great exercise :slight_smile: