PyQt/MiniSipExample¶
Legacy Wiki Page
This page was migrated from the old MoinMoin-based wiki. Information may be outdated or no longer applicable. For current documentation, see python.org.
SIP: Generate Bindings for a class derived from Qt¶
This example shows how to use 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¶
Qt Fragment¶
We define a silly class derived from QString.\
hello.h:
#ifndef _HELLO_H_
#define _HELLO_H_
#include <QString>
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{.backtick}
$ sudo make{.backtick}
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:
1 import os
2 import sipconfig
3 from PyQt4 import pyqtconfig
4
5 # The name of the SIP build file generated by SIP and used by the build
6 # system.
7 build_file = "hello.sbf"
8
9 # Get the PyQt configuration information.
10 config = pyqtconfig.Configuration()
11
12 # Get the extra SIP flags needed by the imported qt module. Note that
13 # this normally only includes those flags (-x and -t) that relate to SIP's
14 # versioning system.
15 qt_sip_flags = config.pyqt_sip_flags
16
17 # Run SIP to generate the code. Note that we tell SIP where to find the qt
18 # module's specification files using the -I flag.
19 os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "-I",
20 config.pyqt_sip_dir, qt_sip_flags, "hello.sip"]))
21
22 # We are going to install the SIP specification file for this module and
23 # its configuration module.
24 installs = []
25
26 installs.append(["hello.sip", os.path.join(config.default_sip_dir,
27 "hello")])
28
29 # Create the Makefile. The QtModuleMakefile class provided by the
30 # pyqtconfig module takes care of all the extra preprocessor, compiler and
31 # linker flags needed by the Qt library.
32 makefile = pyqtconfig.QtCoreModuleMakefile(
33 configuration=config,
34 build_file=build_file,
35 installs=installs
36 )
37
38 # Add the library we are wrapping. The name doesn't include any platform
39 # specific prefixes or extensions (e.g. the "lib" prefix on UNIX, or the
40 # ".dll" extension on Windows).
41 makefile.LFLAGS.append("-L../c++")
42 makefile.extra_libs = ["hello"]
43
44 # Generate the Makefile itself.
45 makefile.generate()
We build the Python-Modul and install it
$ python configure.py{.backtick}
$ make{.backtick}
$ sudo make install{.backtick}
Simple Python Example¶
Now you can use the class inside the python interpreter:
1 import hello
2
3 a=hello.Hello()
4 print a