Create histogram widget in the GUI

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 :

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

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 !

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 :

[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

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.

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 .

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)

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;

1 Like