# Include built-in widget into my own Python extension

**URL:** <https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458>\
**Category:** Development\
**Tags:** python, qt, markups-fiducials\
**Created:** [January 14, 2022, 3:30am UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458 "2022-01-14T03:30:46Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/csatizoltan/32/13807_2.png) [@CsatiZoltan](https://discourse.slicer.org/u/CsatiZoltan)\
**Post date:** [January 14, 2022, 3:30am UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/1 "2022-01-14T03:30:46Z")

</div>

Hi,

I want to develop a simple extension in Python. One step involves placing points on the 3D scene. I realized that the fiducial markup is perfect for my use case, so I want to add that icon, along with the logic it implements, into my own frame. The idea is to simply add the pushbutton widget  
 ![fiducial](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/1/3/13b317f484fd202fd4d9155cef63b0aaf3728860.png)  
and also the list that keeps track of the points the user creates:  
 ![list](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/c/9/c9bf26f2e767b441315a2988f1aabf7df1b12426.png)  
but not the other markup tools (line markup, angle markup, etc.).  
Where can I find these two widgets? I looked for them in the [API docs](https://apidocs.slicer.org/master/group __Slicer__ QtModules__Markups.html) and in the [source code folder](https://github.com/Slicer/Slicer/tree/master/Modules/Loadable/Markups), but could not figure out how to import them as Python modules, so that I can use them from within Python.

Thanks,  
Zoltan

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [January 14, 2022, 10:59pm UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/2 "2022-01-14T22:59:02Z")

</div>

Take a look at [Slicer: qSlicerMarkupsPlaceWidget Class Reference](https://apidocs.slicer.org/master/classqSlicerMarkupsPlaceWidget.html). This widget can handle creating and selecting a fiducial node and toggling placement mode for points. One common stumbling point is that you need to set the mrml scene before the widget will be functional. This can be done in Qt Designer when you are setting up the UI by connecting the module widget’s `mrmlSceneChanged(vtkMRMLScene*)` signal to the MRML widget’s `setMRMLScene(vtkMRMLScene*)` slot. Or it could be done in your module’s `setup` function.

---

<div class="post-metadata">

**Author:** ![Lin](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lin/32/15559_2.png) [@Lin](https://discourse.slicer.org/u/Lin)\
**Post date:** [January 15, 2022, 12:35pm UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/3 "2022-01-15T12:35:18Z")

</div>

That’s very useful to me. I’ll try on my projects.

---

<div class="post-metadata">

**Author:** ![CsatiZoltan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/csatizoltan/32/13807_2.png) [@CsatiZoltan](https://discourse.slicer.org/u/CsatiZoltan)\
**Post date:** [January 16, 2022, 8:38pm UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/4 "2022-01-16T20:38:18Z")

</div>

Thank you. Although I connected the proper signal to the proper slot, the widget is not activated. This is strange because the same workflow works for the [`qSlicerSimpleMarkupsWidget`](https://apidocs.slicer.org/master/classqSlicerSimpleMarkupsWidget.html) widget.

More importantly, can I modify an already existing widget (e.g. by removing certain parts it) without recompilation? I am afraid I can’t because the _Markups_ module is implemented in C++.

---

<div class="post-metadata">

**Author:** ![mikebind](https://avatars.discourse-cdn.com/v4/letter/m/71e660/32.png) [@mikebind](https://discourse.slicer.org/u/mikebind)\
**Post date:** [January 18, 2022, 6:48pm UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/5 "2022-01-18T18:48:13Z")

</div>

Markups place widgets also require an active current markups node to be activated (sorry I omitted that in the first answer, I forgot!). Here is an example of setting that:

```auto
myFiducialNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsFiducialNode', 'F')
w = slicer.qSlicerMarkupsPlaceWidget()
w.setMRMLScene(slicer.mrmlScene)
w.show() # not active yet
w.setCurrentNode(myFiducialNode) # now it is active

```

> [@CsatiZoltan](#):
>
> More importantly, can I modify an already existing widget (e.g. by removing certain parts it) without recompilation? I am afraid I can’t because the _Markups_ module is implemented in C++.

You can control whether parts of the widget are shown or not from Python without recompilation. All qt widgets have `hide()` and `show()` functions, so if you want to hide a part of the custom widget you just need to find that part and invoke its `hide()` method. For example, if you wanted to hide the color picker button, the following would work:

```auto
# w is the markups place widget we set up above
childrenList = w.children()
colorButton = childrenList[8] 
colorButton.hide()

```

I found that the color button was the child with index 8 by just inspecting the list of the widget’s children (I don’t know a more efficient way to do that and I’m not that familiar with Qt, but it is not very much work to look through the full list).

---

<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:** [January 19, 2022, 2:54am UTC](https://discourse.slicer.org/t/include-built-in-widget-into-my-own-python-extension/21458/6 "2022-01-19T02:54:06Z")

</div>

Getting a button by index is very fragile. It is safer to retrieve a button object by name, for example:

```python
colorButton = slicer.util.findChild(w, 'ColorButton')

```

But the safest is to use the public API of the widget. For example, you can make the widget only show a place button like this:

```python
w.buttonsVisible = False
w.placeButton().show()

```

Not all the buttons are exposed on the public API. We could change that, but was not needed until now.
