= SIP: Generate Bindings for a class derived from Qt = This example shows how to use [[http://www.riverbankcomputing.co.uk/sip/index.php|SIP]] for generating bindings to Qt c++ modules. The Example shown in the sip-documentation is not complete and perhaps we can prevent you to waste hours of time to get things run. == Before You Begin == 1. Install [[http://www.trolltech.com|Qt4]] 1. Build and install [[http://www.riverbankcomputing.com/software/sip/intro|SIP]] 1. Build and install [[http://www.riverbankcomputing.com/software/pyqt/intro|PyQt]] == Qt Fragment == We define a silly class derived from QString.<
> hello.h: {{{ #ifndef _HELLO_H_ #define _HELLO_H_ #include class Hello : public QString { public: Hello(); }; #endif }}} hellp.cpp: {{{ #include "hello.h" Hello::Hello() { append("Hello World"); } }}} hello.pro {{{ TEMPLATE = lib CONFIG += qt warn_on release HEADERS = hello.h SOURCES = hello.cpp TARGET = hello DESTDIR = /usr/lib }}} Now We call qmake for qt4 to generate the makefile, then make to build the example and install it. DESTDIR describes the directory in which the executable or binary file will be placed. DESTDIR ist Platform depended. `$ qmake`<
> `$ sudo make` == Telling SIP what to do == We need two files.<
> hello.sip: {{{ %Module hello 0 %Import QtCore/QtCoremod.sip class Hello : QString { %TypeHeaderCode #include "../c++/hello.h" %End public: Hello(); }; }}} configure.py: {{{#!python import os import sipconfig from PyQt4 import pyqtconfig # The name of the SIP build file generated by SIP and used by the build # system. build_file = "hello.sbf" # Get the PyQt configuration information. config = pyqtconfig.Configuration() # Get the extra SIP flags needed by the imported qt module. Note that # this normally only includes those flags (-x and -t) that relate to SIP's # versioning system. qt_sip_flags = config.pyqt_sip_flags # Run SIP to generate the code. Note that we tell SIP where to find the qt # module's specification files using the -I flag. os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "-I", config.pyqt_sip_dir, qt_sip_flags, "hello.sip"])) # We are going to install the SIP specification file for this module and # its configuration module. installs = [] installs.append(["hello.sip", os.path.join(config.default_sip_dir, "hello")]) # Create the Makefile. The QtModuleMakefile class provided by the # pyqtconfig module takes care of all the extra preprocessor, compiler and # linker flags needed by the Qt library. makefile = pyqtconfig.QtCoreModuleMakefile( configuration=config, build_file=build_file, installs=installs ) # Add the library we are wrapping. The name doesn't include any platform # specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the # ".dll" extension on Windows). makefile.LFLAGS.append("-L../c++") makefile.extra_libs = ["hello"] # Generate the Makefile itself. makefile.generate() }}} We build the Python-Modul and install it `$ python configure.py`<
> `$ make`<
> `$ sudo make install` == Simple Python Example == Now you can use the class inside the python interpreter: {{{#!python import hello a=hello.Hello() print a }}}