Qt for Python: under the hood

Today I would like to tell you about the "Qt for Python bindings" generation process.

The following diagram shows the most general interaction of the internal process that The Qt Company is using to provide the PySide2 module:


When the PySide project was launched back in 2009, the team decided to use external tools to generate Python bindings from Qt C++ headers.
One of the main concerns, besides using a tool that properly handles all the Qt C++ constructs, was the size of the final packages.
The previous choice was using templates excessively, hence another alternative was required.
After analyzing a few other options the team decided to write their own generator, Shiboken.

Shiboken uses an ApiExtractor library (based on an old project called QtScriptGenerator) to parse the Qt Headers and get the information of all the Qt classes, using Clang.
This step is not Qt-dependent, so it can be use for other C++ projects as well.

Additionally, Shiboken has a Typesystem (based on XML) which allows modifying the obtained information to properly represent and manipulate the C++ classes into the Python World.

Through this Typesystem we can remove and add methods to certain classes, and even modify the arguments of each function, which is really necessary when both C++ and Python collide and a decision needs to be made to properly handle the data structures or types.

The outcome of this process is a set of wrappers written in CPython, which allow us to easily compile and then provide you the python module called PySide2.

Official documentation related to the ApiExtractor and Shiboken will be included in the official Qt for Python documentation, so you can get involved in their development, and if you are curious how Shiboken works, stay tuned for the future blog posts!

Blog Topics: