# qMRMLSubjectHierarchyComboBox usage

**URL:** <https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763>\
**Category:** Development\
**Created:** [July 25, 2017, 5:06pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763 "2017-07-25T17:06:06Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 25, 2017, 5:06pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/1 "2017-07-25T17:06:06Z")

</div>

How to get notified in Python when `currentItemChanged` fires? I tried this:

```
# myCB1 is qMRMLNodeComboBox
# myCB2 is qMRMLSubjectHierarchyComboBox
myCB1.connect('currentNodeChanged(vtkMRMLNode*)', self.updateMRMLFromGUI)
myCB2.connect('currentItemChanged(int)', self.updateMRMLFromGUI)

def updateMRMLFromGUI(self, **unused):
  # gets called for myCB1, but not for myCB2

```

How to make only volumes selectable, and not studies or patients? For `qMRMLNodeComboBox` this `.ui` fragment only shows volumes:

```
   <widget class="qMRMLNodeComboBox" name="myCB1">
    <property name="nodeTypes">
     <stringlist>
      <string>vtkMRMLScalarVolumeNode</string>
     </stringlist>
    </property>

```

Is there an equivalent or similar thing for `qMRMLSubjectHierarchyComboBox` either as `.ui` fragment or Python snippet?

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 25, 2017, 6:34pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/2 "2017-07-25T18:34:13Z")

</div>

First question:  
`self.usPatientItemCombobox.connect('currentItemChanged(vtkIdType)', self.onUSPatientSelectionChanged)`

Second question:  
If you only want to be able to select volumes, then simply use qMRMLNodeComboBox. If you want to see the hierarchy as well, but only have the volumes in the hierarchy show up, then you can easily add a filter similar to the attribute filter in the combobox class

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSubjectHierarchyComboBox.h#L81>

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 25, 2017, 9:12pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/3 "2017-07-25T21:12:13Z")

</div>

I have tried `myCB2.connect('currentItemChanged(vtkIdType)', self.updateMRMLFromGUI)` first, and since that didn’t work I also tried `int` to which `vtkIdType` resolves. Does Python have a 64-bit int?

I have seen the method [setAttributeFilter](http://apidocs.slicer.org/master/classqMRMLSubjectHierarchyComboBox.html#aa74541aabd9f1294b6e91a4da00d4924), but no example of its usage. Can you provide a few example calls to this method, even if it doesn’t accomplish what I want?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 26, 2017, 1:03am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/4 "2017-07-26T01:03:17Z")

</div>

> [@dzenanz](#):
>
> 'currentItemChanged

I agree, it would be great to have some examples in SH Python tests and a few words about this in the class documentation would be useful, too.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 26, 2017, 1:00pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/5 "2017-07-26T13:00:17Z")

</div>

Not sure what you mean by “a few words”, but there is as much in the documentation as there can be for an extremely simple feature like this I think:

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSubjectHierarchyTreeView.h#L85-L88>

  
and  

> <https://github.com/Slicer/Slicer/blob/master/Modules/Loadable/SubjectHierarchy/Widgets/qMRMLSubjectHierarchyComboBox.h#L77-L80>

  
Would you add something else?

I’ll add a test about it to the SH generic test. Apparently it started failing last night, so I need to check up on it anyway.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 26, 2017, 2:56pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/6 "2017-07-26T14:56:33Z")

</div>

Example in new test section:  
[https://github.com/Slicer/Slicer/commit/e66e3b08e35384526528e6ae678e9ec9f079f286#diff-8cdc75584ed1fbe2b0be1e655a2f22a1R352](https://github.com/Slicer/Slicer/commit/e66e3b08e35384526528e6ae678e9ec9f079f286#diff-8cdc75584ed1fbe2b0be1e655a2f22a1R352)

---

<div class="post-metadata">

**Author:** ![jcfr](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/jcfr/32/17825_2.png) [@jcfr](https://discourse.slicer.org/u/jcfr)\
**Post date:** [July 26, 2017, 3:10pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/7 "2017-07-26T15:10:33Z")

</div>

@dzenanz Where do you think we should link or reference existing documentation and code samples to streamline your developer experience ?

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 26, 2017, 6:05pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/8 "2017-07-26T18:05:54Z")

</div>

@cpinter Thanks, that test is very useful as an example.

@jcfr I expected the examples to be either on the [labs page](https://www.slicer.org/wiki/Documentation/Labs/SubjectHierarchy) or in the [script repository](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Subject_hierarchy). I will add Csaba’s test as an example in the script repository.

Still, `currentItemChanged` does not trigger for me. Parts of actual code:

```
self._get('projectionImage').connect('currentItemChanged(vtkIdType)', self.shChanged) # setting updateMRMLFromGUI directly as slot doesn't work either
self._get('outputImage').connect('currentNodeChanged(vtkMRMLNode*)', self.updateMRMLFromGUI) # gets triggered properly

def shChanged(self, shID):
    self.updateMRMLFromGUI(shID)
    print "Triggered: "+str(shID)
  
def updateMRMLFromGUI(self, **unused):
    # handle the change
```

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 26, 2017, 6:22pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/9 "2017-07-26T18:22:43Z")

</div>

Link to newly added docs: [https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Filter\_nodes\_in\_TreeView\_or\_ComboBox](https://www.slicer.org/wiki/Documentation/Nightly/ScriptRepository#Filter_nodes_in_TreeView_or_ComboBox)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 26, 2017, 6:42pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/10 "2017-07-26T18:42:34Z")

</div>

Thanks! I have two comments:

1. I think the Qt designer plugins are an irrelevant detail for this example
2. The entities that you show and in this case filter in SH are not nodes, but they are items. It’s important, because for many of the items there is no corresponding MRML node. This may actually confuse people who want to filter in node attribute, when in reality the filter applies on SH item attributes.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 26, 2017, 6:44pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/11 "2017-07-26T18:44:10Z")

</div>

I quickly fixed it myself.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 26, 2017, 7:51pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/12 "2017-07-26T19:51:25Z")

</div>

Thanks @cpinter for docs fix.

Can you provide declaration for `onUSPatientSelectionChanged`, corresponding to this code fragment?

```
self.usPatientItemCombobox.connect('currentItemChanged(vtkIdType)', self.onUSPatientSelectionChanged)
```

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 26, 2017, 8:55pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/13 "2017-07-26T20:55:33Z")

</div>

Of course, the example is from the SegmentRegistration extension:

> <https://github.com/SlicerRt/SegmentRegistration/blob/master/ProstateMRIUSContourPropagation/ProstateMRIUSContourPropagation.py#L283-L290>

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 27, 2017, 10:02pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/14 "2017-07-27T22:02:46Z")

</div>

Filtering does not work, and copy-pasting from SegmentRegistration didn’t help for event triggering:

```
def __init__ (self, parent=None):
  slicer.qMRMLWidget. __init__ (self, parent)
  slicer.util.VTKObservationMixin. __init__ (self)
  ...
  self._get('projectionImage').connect('currentItemChanged(vtkIdType)', self.shChanged)
  # self._get('projectionImage').setAttributeFilter('DICOM.Modality','NM') #combobox empty if enabled
  self._get('outputImage').connect('currentNodeChanged(vtkMRMLNode*)', self.updateMRMLFromGUI)
  
  # copy-pasted code, combobox is displayed and shows the hierarchy properly
  self.usPatientItemCombobox = slicer.qMRMLSubjectHierarchyComboBox()
  self.usPatientItemCombobox.name = "usPatientItemCombobox"
  self.usPatientItemCombobox.setMRMLScene( slicer.mrmlScene )
  self._get('ioNodes').layout().addRow('US patient', self.usPatientItemCombobox)
  self.usPatientItemCombobox.connect('currentItemChanged(vtkIdType)', self.onUSPatientSelectionChanged)
  ...
  
def onUSPatientSelectionChanged(self, usPatientShItemID):
  print "usPatientShItemID: "+str(usPatientShItemID)

```

I will try to create a minimum working example to diagnose what is going on.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 28, 2017, 12:13am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/15 "2017-07-28T00:13:24Z")

</div>

When you say something “doesn’t work”, please tell us how it doesn’t work. See [https://www.slicer.org/wiki/Documentation/Nightly/Report\_a\_problem#Error\_report\_contents](https://www.slicer.org/wiki/Documentation/Nightly/Report_a_problem#Error_report_contents)

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [July 28, 2017, 2:05pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/16 "2017-07-28T14:05:22Z")

</div>

> [@dzenanz](#):
>
> self.\_get(‘projectionImage’).setAttributeFilter(‘DICOM.Modality’,‘NM’)

With this line present, the projectionImage `qMRMLSubjectHierarchyComboBox` doesn’t display any nodes

 ![36](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/5/85605413ade8ea5f54ca376e7438082b6bb9152a.png)  
even if there are nodes which have NM modality:  
 ![58](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/2/9/29521d32edf9932644e2128c72d0a552a2d543ab.png)

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [July 28, 2017, 2:51pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/17 "2017-07-28T14:51:23Z")

</div>

Indeed, the screenshot shows that the item does have that attribute. What happens if you set the same filter on the SH tree view? You can get it like this:  
dmw=slicer.modules.data.widgetRepresentation()  
sht=slicer.util.findChildren(dmw,className=‘qMRMLSubjectHierarchyTreeView’)[0]

If I create a combobox like this (I had a CT in the scene), it works with the latest version:  
c=slicer.qMRMLSubjectHierarchyComboBox()  
c.setMRMLScene(slicer.mrmlScene)  
c.show()  
c.setAttributeFilter(‘DICOM.Modality’,‘CT’)

Basic question but are you sure you set the scene to the combobox?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [July 28, 2017, 4:52pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/18 "2017-07-28T16:52:01Z")

</div>

Another thing to check: Try if it works if you set the attribute before you create the widget (the order should not matter but maybe there is a bug and the tree is not re-filtered when an attribute is changed).

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [August 1, 2017, 10:52pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/19 "2017-08-01T22:52:24Z")

</div>

@lassoan Even if data is loaded (and hence attributes are set) before my widget is created/accessed, I did not notice any change.

I created the minimum working example in this [commit](https://github.com/dzenanz/Slicer/commit/76f880c2661267aff7cf674d5b08aa203262f959). The [problematic](https://github.com/dzenanz/Slicer/blob/76f880c2661267aff7cf674d5b08aa203262f959/Modules/Scripted/ScriptedMWE/ScriptedMWEWidgets/ScriptedMWEWidget.py#L16-L19) lines are 16-19.

---

<div class="post-metadata">

**Author:** ![dzenanz](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/dzenanz/32/1992_2.png) [@dzenanz](https://discourse.slicer.org/u/dzenanz)\
**Post date:** [August 4, 2017, 5:19pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763/20 "2017-08-04T17:19:57Z")

</div>

@cpinter Have you tried my [MWE](https://github.com/dzenanz/Slicer/blob/76f880c2661267aff7cf674d5b08aa203262f959/Modules/Scripted/ScriptedMWE/ScriptedMWEWidgets/ScriptedMWEWidget.py#L16-L19)?

[Next page](https://discourse.slicer.org/t/qmrmlsubjecthierarchycombobox-usage/763.md?page=2)
