PySide Binding Generation Tutorial: Module 2 Binding libfoo using Shiboken

From Qt Wiki
Revision as of 16:21, 14 January 2015 by Maintenance script (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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:

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: 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:

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<code> directory which is a Good Thing to have.

Categories: