English French [qt-devnet.developpez.com]

Binding libfoo using Shiboken

In order to create bindings for a library based on Qt, a number of components must be available on the system:

  • Qt library (with headers and pkg-config .pc files for development – the -dev packages in a Debian-like distribution).
  • Qt Python bindings made with Shiboken, i.e. PySide.
  • Type systems for the Qt Python bindings.
  • Headers for the library to be wrapped.

With the items listed above the developer must collect information used by the generator to create the binding source code:

  • Type system file describing the way the binding must be done.
  • global.h including all the libfoo headers and defining required macros.
  • A build system to direct the process of generating, compiling and linking the binding.

libfoo bindings with Makefile

The directory structure and contents for the Makefile binding project could be something like the tree shown below:

  1. foobinding-makefile/
  2. |-- foo/
  3. |-- global.h
  4. |-- Makefile
  5. |-- pyside_global.h
  6. `-- typesystem_foo.xml

The foobinding-makefile directory should contain the global.h, which is a central point to include all the libfoo headers (ok, it’s just one header, but a real life library will certainly have more headers), it could also contain #define clauses that will influence the parsing of the headers by the generator. Nevermind the pyside_global.h file, it’ll be explained later. The typesystem_foo.xml describes how to export the wrapped C++ library to Python as explained in Module 3 [developer.qt.nokia.com].

The foo directory is the place where the generated sources will be placed. It starts empty and its name will be the same as the package name found in the type system file:

  1. <typesystem package="foo">

If there is any need for handwritten source code longer than a couple of lines, making it inconvenient to put them in the type system xml file, the sources could be orderly placed in a glue directory. For the foo bindings no custom code will be needed.

When writing the type system file (more on this later) there is no need to refer to the other required type system files with absolute paths, the locations where they can be found could be passed to the generator through a command line option (—typesystem-paths=PATH1:PATH2:[…]) or the environment variable TYPESYSTEMPATH.

libfoo bindings with CMake

The directory structure for the CMake based libfoo bindings is a bit more elaborate:

  1. foobinding-cmake/
  2. |-- CMakeLists.txt
  3. |-- foo/
  4. |   |-- CMakeLists.txt
  5. |   |-- global.h
  6. |   |-- pyside_global.h
  7. |   `-- typesystem_foo.xml
  8. `-- tests/
  9.     `-- CMakeLists.txt
  10.         `-- math_test.py

This structure follows the one used in the PySide bindings: a root directory, one directory for the binding information (essentially the same as the Makefile example), and a tests directory which is a Good Thing™ to have.

Categories:

  • PySide_Binding_Generation_Tutorial
  •