Write python bindings


I have some Qt derived classes (widgets) written in C++ and used in SlicerCAT and I would like to bind them in python.

I have some experience of working with pybind11 and I do use it in my libraries that are not connected to Qt.
There is also automatic code generator rosetta-binder based on pybind11.

I also know that Qt also provides its method to convert C++ to python using Shiboken tool. Though I have not tested it yet.

So I would appreciate if someone could give an advice wich way is better and what should I use to convert C++ Qt based widget to python and use it in SlicerCAT

Is there a reason you choose to create your Qt based widget in C++ rather than creating the widget in python? Are you using it in other C++ code?

You can create a custom QWidget from the python interface for use in other python code.

1 Like

That is the question that I ask myself.
I think there are two reasons:

  1. I already implemented them in C++
  2. I think that in the future I encounter a situation when I will need to bind C++ class to python and thus it means that I will simply postpone this task if I decide not to use bindings now.

@keri did you look at how other Qt classes are wrapped in Slicer using PythonQt? This would be more consistent, and hopefully easier, than introducing another python wrapping method. I haven’t tried it in SlicerCAT, but I believe all the same functionality is available.

1 Like


I have made an attempt but didn’t find a file (or class) where it is done. If you know an example in Slicer please say where to look for it.

Also as I know PyQt, PySide2 (official) and PythonQt they are all different projects am I right? Which ones Slicer is using?

Slicer uses PythonQt very extensively so you can look at any QObject or QWidget subclass in Slicer and the API will be exposed to Python through PythonQt. Basically, the public interface is wrapped, so Signals, Slots, and Properties are all available. In addition methods marked with the Q_INVOKABLE macro will also be wrapped. All the python-related developer tutorials make use of this.

1 Like

thank you! I will focus on this and try to sort it out