# Notification when \`qMRMLSliceWidget\` added

**URL:** <https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252>\
**Category:** Development\
**Created:** [October 20, 2021, 12:06am UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252 "2021-10-20T00:06:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [October 20, 2021, 12:06am UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/1 "2021-10-20T00:06:07Z")

</div>

Hi,

Is there some kind of signal when `qMRMLSliceWidget` added?

I’m trying to add a toolbutton to it and there is no preblem to do that on existing `qMRMLSliceWidget` (there is an example in scrit repo) but when new slice widget is added I need to add the same button to it.

---

<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:** [October 20, 2021, 4:53am UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/2 "2021-10-20T04:53:25Z")

</div>

You can observe the [layout changed signal of the layout manager](https://apidocs.slicer.org/master/classqMRMLLayoutManager.html#ac5c1ebc99fe71f02ea72894df6cf56d8).

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [October 20, 2021, 1:00pm UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/3 "2021-10-20T13:00:05Z")

</div>

Thank you for help,

I also found [qMRMLLayoutViewFactory::viewCreated(QWidget\*)](https://apidocs.slicer.org/master/classqMRMLLayoutViewFactory.html#af27583d80ef3b4ea0cd6978e092aefc0) (though as I understood it always `emit nullptr`).

And I reach the factory with:

```cpp
  qMRMLLayoutViewFactory* factory = app->
      layoutManager()->mrmlViewFactory("vtkMRMLSliceNode");

```

What is the preferred solution: connect to layout manager or to factory?

---

<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:** [October 20, 2021, 2:29pm UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/4 "2021-10-20T14:29:53Z")

</div>

Adding a connection to the `viewCreated` signal to every view types you are interested in should work, too. That signal emits a valid pointer to the widget that has just been created.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [October 20, 2021, 3:01pm UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/5 "2021-10-20T15:01:18Z")

</div>

> [@lassoan](#):
>
> That signal emits a valid pointer to the widget that has just been created.

I may be mistaken but in the Slicer’s source code [view factory viewCreated(viewWidget)](https://github.com/Slicer/Slicer/blob/11d0ca30162e9fd0315997dd41908354376b52a4/Libs/MRML/Widgets/qMRMLLayoutViewFactory.cxx#L419) signal is emitting with widget that is created with [`createViewFromNode(vtkMRMLAbstractViewNode* viewNode)` function that always returns nullptr](https://github.com/Slicer/Slicer/blob/11d0ca30162e9fd0315997dd41908354376b52a4/Libs/MRML/Widgets/qMRMLLayoutViewFactory.cxx#L454-L458)

Is implementation of `QWidget* qMRMLLayoutViewFactory::createViewFromNode(vtkMRMLAbstractViewNode* viewNode)` is correct?

P.S. I’m able to catch the `viewCreated` signal but it is sent with `nullptr` parameter:

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/0/9/09052e34e60c5099108a7eadc3e336fd0b27b216.jpeg)

---

<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:** [October 20, 2021, 3:33pm UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/6 "2021-10-20T15:33:38Z")

</div>

`createViewFromNode` method is overridden in concrete factory classes (see qMRMLLayoutManager.cxx). You can add a breakpoint at `emit viewCreated(viewWidget);` and see that `viewWidget` is not a nullptr.

Most likely something is wrong where you set up the connection between the signal and your slot.

---

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [October 20, 2021, 4:04pm UTC](https://discourse.slicer.org/t/notification-when-qmrmlslicewidget-added/20252/7 "2021-10-20T16:04:09Z")

</div>

I see now, thank you for explanation
