# qMRMLSubjectHierarchyTreeView::setCurrentItem proposal

**URL:** <https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301>\
**Category:** Development\
**Created:** [September 1, 2025, 2:45pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301 "2025-09-01T14:45:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [September 1, 2025, 2:45pm UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/1 "2025-09-01T14:45:26Z")

</div>

Dear Slicer developers,

I would like to get some feedback on a potential fix in qMRMLSubjectHierarchyTreeView: [ENH: qMRMLSubjectHierarchyTreeView: call setCurrentIndex rather than … · Slicer/Slicer@1fdee41 · GitHub](https://github.com/Slicer/Slicer/commit/1fdee41a7c916c79957f608f1fb85106d623bbfa)

In my custom 3d slicer app I load a case which:

- contains only 2 volumes in the data tree
- sets the current item of a qMRMLSubjectHierarchyCombobox to the last volume (this combobox is used in my custom module)

**Scenario 1:**

- Load the case.
- Remove the last volume from the data tree.
- Result: the combobox displays None.

**Scenario 2:**

- Load the case.
- Click on the combobox and click on the last volume (which is already selected)
- Remove the last volume from the data tree.
- Result: the combobox displays the first volume.

**Analysis**

When the last volume is removed from the data tree, this method is called:

QAbstractItemView::rowsAboutToBeRemoved

This method checks the current index:

if the current index is valid then the next index is selected (previous volume =\> scenario 2)

if the current index is NOT valid then no index is selected (None =\> scenario 1)

In scenario 1 there is no current index because qMRMLSubjectHierarchyCombobox simply calls qMRMLSubjectHierarchyTreeView which calls only selectionModel()-\>select which does not set the current index.

As you can see in this commit , this-\>selectionModel()-\>setCurrentIndex selects the item AND set the current index.

Do you see any case where an item must be selected without being the current index ?

Do you see any other issue ?

Best regards

---

<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:** [September 2, 2025, 5:23am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/2 "2025-09-02T05:23:48Z")

</div>

Both behaviors are very useful. The developer should be able to choose between them using the [`noneEnabled` flag the same way as in the node combobox](https://apidocs.slicer.org/main/classqMRMLNodeComboBox.html#a54e8994dfb8e4cff016f1c082cb9b385).

---

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [September 2, 2025, 8:28am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/3 "2025-09-02T08:28:24Z")

</div>

noneEnabled is already set to True in my qMRMLSubjectHierarchyCombobox.

Unfortunately I do not find any way to create a reproducer in 3D Slicer.

According to me, removing a row of the subject hierarchy tree should always lead to the same behavior in a qMRMLSubjectHierarchyCombobox, it should not change if the user already clicked on an item or not.

Please let me know if this commit could be interesting or if I have to implement a fix only in my custom app.

Regards

---

<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:** [September 2, 2025, 11:28am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/4 "2025-09-02T11:28:19Z")

</div>

> [@aymeric.chataigner](#):
>
> behavior in a qMRMLSubjectHierarchyCombobox … should not change if the user already clicked on an item or not.

Yes, I agree. If this is not the case then it is a bug that should be fixed.

This widget is intended to mirror the behavior of qMRMLNodeComboBox, making it suitable as a drop-in replacement.

---

<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:** [September 3, 2025, 9:41am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/5 "2025-09-03T09:41:28Z")

</div>

> [@aymeric.chataigner](#):
>
> simply calls qMRMLSubjectHierarchyTreeView which calls only selectionModel()-\>select which does not set the current index.

If this is the case, then I think it is a bug, and your change is well justified. I can’t think of a case when we want to set selection only in the view without setting the same selection in the model.

Thanks for the investigation!

---

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [September 4, 2025, 7:41am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/6 "2025-09-04T07:41:51Z")

</div>

> [@cpinter](#):
>
> ction only in the view without setting the same selection in the model.

Here is the pull request: [BUG: Manage correctly row deletion in qMRMLSubjectHierarchyCombobox by achataigner · Pull Request #8692 · Slicer/Slicer · GitHub](https://github.com/Slicer/Slicer/pull/8692)

---

<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:** [September 5, 2025, 10:33am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/7 "2025-09-05T10:33:02Z")

</div>

(I cannot approve the PR unless you request me specifically as reviewer, so need to wait until someone with higher rights approves it)

---

<div class="post-metadata">

**Author:** ![aymeric.chataigner](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/aymeric.chataigner/32/4977_2.png) [@aymeric.chataigner](https://discourse.slicer.org/u/aymeric.chataigner)\
**Post date:** [September 8, 2025, 7:39am UTC](https://discourse.slicer.org/t/qmrmlsubjecthierarchytreeview-setcurrentitem-proposal/44301/8 "2025-09-08T07:39:31Z")

</div>

Ok I just added a comment in the PR to know if you can review it
