# Create histogram widget in the GUI

**URL:** <https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919>\
**Category:** Support\
**Tags:** python\
**Created:** [June 8, 2023, 1:54pm UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919 "2023-06-08T13:54:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tux](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tux](https://discourse.slicer.org/u/tux)\
**Post date:** [June 8, 2023, 1:54pm UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919/1 "2023-06-08T13:54:40Z")

</div>

Os : Ubuntu 20.04.6  
Slicer version : 5.2.2

Hello !  
I am currently searching how to create an histogram but as a widget inside my module GUI like the one in the volume module :

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/a/5/a5d57d5d2547ec64c472ede0ccd15592c91686a0.png)

I already have one in the “visualisation page” (i don’t know how is this called) like this :

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/b/9/b9bac5a65c5a2590071dcba0b9e6c15832373db4.png)

so i already have the code to create the histogram with numpy.  
I tried to look at the code of the Volume module but i don’t know anything about c++ so i couldn’t find what i want.  
Can someone hellp me to know if it’s at least possible to create this widget with python ? And if so, how could i do ?  
Thank you !

---

<div class="post-metadata">

**Author:** ![tux](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tux](https://discourse.slicer.org/u/tux)\
**Post date:** [June 9, 2023, 9:32am UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919/2 "2023-06-09T09:32:58Z")

</div>

hi again !  
I found the widget that is used in the volume module (the `ctkTransferFunctionView`) and i understood that i need to draw inside it with QPainter but i can’t find how to draw.  
I think i almost got it with `widget.paintEvent()` and a event i created myself, but here is the error i got :

```auto
[Qt] QWidget::paintEngine: Should no longer be called
[Qt] QPainter::begin: Paint device returned engine == 0, type: 1
[Qt] QPainter::setRenderHint: Painter must be active to set rendering hints
[Qt] QPainter::setRenderHint: Painter must be active to set rendering hints
[Qt] QPainter::setWorldTransform: Painter not active
[Qt] QPainter::worldTransform: Painter not active
[Qt] QPainter::save: Painter not active
[Qt] QPainter::restore: Unbalanced save/restore
[Qt] QPainter::setWorldTransform: Painter not active
[Qt] QPainter::end: Painter not active, aborted

```

And i can’t set the painter active no matter what i do

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 9, 2023, 1:37pm UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919/3 "2023-06-09T13:37:52Z")

</div>

It looks like you are on the right track. The CTK widget could no doubt be improved to make it more flexible. It would be good if you could look at how it’s implemented and propose changes to make it work better for what you have in mind.

---

<div class="post-metadata">

**Author:** ![tux](https://avatars.discourse-cdn.com/v4/letter/t/f4b2a3/32.png) [@tux](https://discourse.slicer.org/u/tux)\
**Post date:** [June 9, 2023, 2:12pm UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919/4 "2023-06-09T14:12:49Z")

</div>

I can’t find the source code of the function `paintEvent()` but from what i understand, i think the problem come from an “recent” update where now you need to use the `beginNativePainting()` and `endNativePainting()` instead of the old ones `begin()` and `end()`.  
But as already said, i don’t know c++ and the source code is probably in this language since i tried a `grep -ri 'paintEvent'` in the `/Slicer-5.2.2-linux-amd64/lib/Slicer-5.2` folder to see where the source code could be, and every file returned were `*.so` .

---

<div class="post-metadata">

**Author:** ![pieper](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/pieper/32/8_2.png) [@pieper](https://discourse.slicer.org/u/pieper)\
**Post date:** [June 9, 2023, 2:21pm UTC](https://discourse.slicer.org/t/create-histogram-widget-in-the-gui/29919/5 "2023-06-09T14:21:22Z")

</div>

Yes, the Qt/CTK code is C++ so if you want to reuse or extend then you will need to learn C++ (if your goal is to develop code it’s good to know C++ so you understand how things work and what can and can’t be done in which languages)

> **[CTK/Libs/Widgets at master · commontk/CTK](https://github.com/commontk/CTK/tree/master/Libs/Widgets)**
>
> master/Libs/Widgets

If you want to avoid all that, you could just implement a fresh widget in python that does exactly what you need. You can still use the Qt paint methods. Something like this;

> <https://github.com/Slicer/Slicer/blob/main/Modules/Scripted/WebServer/WebServerLib/SlicerRequestHandler.py#L1082-L1107>
