Qt for Python: under the hood

Published Thursday May 24th, 2018
6 Comments on Qt for Python: under the hood
Posted in Qt for Python

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!

Do you like this? Share it
Share on LinkedInGoogle+Share on FacebookTweet about this on Twitter

Posted in Qt for Python


Peter says:

Could this process also be used to make Java bindings? Or even better, is The Qt Company considering Java bindings?

Cristián Cristián says:

The idea of the process, sure, but the problem is that you would need to change the whole generator to adapt the generated code to Java. The extraction of the Qt API by ApiExtractor is compatible, but again, you will need to modify the equivalence between C++ to Java.
We are not considering bindings for Java, but there use to be a set of bindings called Qt Jambi, but I think the development is stalled.
Maybe the community would like to port the latest version of Qt Jambi to use Qt 5.11

Peter says:

Why not provide a C wrapper/interface/binding? IMHO this would be much more useful for *all* languages then just Python. 😉 Every language I know is able to call C.

Cristián Cristián says:

Because it’s out of our scope, the goal of the generator was only Python.
But since it’s an open source project, maybe the community wants to contribute generalizing it to support *all* languages 😉

Godwin says:

Thanks, this will help me, if I am able to succeed in using it, to stay update with qt releases. Since python bindings are often not up to date with qt or sometimes Anaconda releases doesn’t yet support the current version of the python binding

Cristián Cristián says:

I’m sure that the people behind the Anaconda’s packages will update them once the official wheels are out.

Commenting closed.

Get started today with Qt Download now