How to customize SubjectHierarchyTreeView?

Hi!
I want to customize a SubjectHierarchyTreeView Widget in a module.

Things to change:

  • Filter Model nodes: made

self.uit.SubjectHierarchyTreeView.nodeTypes = [“vtkMRMLModelNode”]

  • Change header text: “Node” → “Model”
  • Hide columns: “ID”, “color”
  • Change colums order: Put View/Hide first one
  • Add new columns: should be lovely to add an “Opacity slider”, something like Segementations Module one.

Slicer ver: 4.11.20200930 r29402 / 002be18
OS: Win10

Thanks on advace!

subjectHierarchyTreeView.setColumnHidden(subjectHierarchyTreeView.model().idColumn, True)
subjectHierarchyTreeView.setColumnHidden(subjectHierarchyTreeView.model().colorColumn, True)

Probably something like this:
subjectHierarchyTreeView.model().setHorizontalHeaderLabels(['Models', ...])

I believe you can do it by calling functions like setNameColumn with the desired column index.

I’m afraid for this you’ll need to extend or subclass the qMRMLSubjectHierarchyModel class.

1 Like

Hi @cpinter!
Thanks to your answare, I´ve able to get this result:

image

This is my code:

self.ui.SubjectHierarchyTreeView.nodeTypes = [“vtkMRMLModelNode”]
self.ui.SubjectHierarchyTreeView.setColumnHidden(self.uit.SubjectHierarchyTreeView.model().idColumn, True)
self.ui.SubjectHierarchyTreeView.setColumnHidden(self.uit.SubjectHierarchyTreeView.model().colorColumn, True)
self.ui.SubjectHierarchyTreeView.setColumnHidden(self.uit.SubjectHierarchyTreeView.model().transformColumn, True)
self.ui.SubjectHierarchyTreeView.model().setHorizontalHeaderLabels([‘Models’, …])
self.ui.SubjectHierarchyTreeView.setMRMLScene( slicer.mrmlScene )

I havent been able to change columns order after many attempts with different methods from qMRMLSubjectHierarchyTreeView Class and qMRMLSubjectHierarchyModel documentation.

Thank you very much for your help!

You can show/hide and change the order of columns by specifying colorColumn, descriptionColumn, etc. values. See documentation.

Sorry @lassoan . I don´t know how to do it…
Error message:

line 200, in setup self.ui.SubjectHierarchyTreeView.model().setVisibilityColumn(0)
AttributeError: qMRMLSubjectHierarchyModel has no attribute named ‘setVisibilityColumn’

colorColumn is a property. You can set it as:

self.ui.SubjectHierarchyTreeView.model().visibilityColumn = 0

This documentation page can help in interpreting the API documentation: https://slicer.readthedocs.io/en/latest/developer_guide/api.html#doxygen-style-documentation

1 Like

I´ve triyed:

self.uit.SubjectHierarchyTreeView.model().visibilityColumn = 0
self.uit.SubjectHierarchyTreeView.model().nameColumn = 1
self.uit.SubjectHierarchyTreeView.model().descriptionColumn = 2

But result is the same:

image

Sorry to waste your precious time…!

It works for me. Note that you need to change the headers label and icons with a separate call (it could be nice if that was updated automatically, if you have time to implement this then a pull request would be welcome):

tv=slicer.qMRMLSubjectHierarchyTreeView()
tv.model().visibilityColumn=3
tv.model().colorColumn=2
tv.setMRMLScene(slicer.mrmlScene)
tv.model().setHorizontalHeaderLabels(['a','b','c','d','e','f'])
tv.show()
1 Like

Ok. My problem was the headers order and columns width. Items reorder in new columns distribution but but headers stay in original order.

I´d like to help but it´s too much for my current poor code skills…

Thanks to all.