PyQt/MiniSipExample

This wiki is in the process of being archived due to lack of usage and the resources necessary to serve it — predominately to bots, crawlers, and LLM companies. Edits are discouraged.
Pages are preserved as they were at the time of archival. For current information, please visit python.org.
If a change to this archive is absolutely needed, requests can be made via the infrastructure@python.org mailing list.

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

  1. Install Qt4

  2. Build and install SIP

  3. Build and install PyQt

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

   1 import os
   2 import sipconfig
   3 from PyQt4 import pyqtconfig
   4 
   5 
   6 
   7 build_file = "hello.sbf"
   8 
   9 
  10 config = pyqtconfig.Configuration()
  11 
  12 
  13 
  14 
  15 qt_sip_flags = config.pyqt_sip_flags
  16 
  17 
  18 
  19 os.system(" ".join([config.sip_bin, "-c", ".", "-b", build_file, "-I", 
  20 config.pyqt_sip_dir, qt_sip_flags, "hello.sip"]))
  21 
  22 
  23 
  24 installs = []
  25 
  26 installs.append(["hello.sip", os.path.join(config.default_sip_dir, 
  27 "hello")])
  28 
  29 
  30 
  31 
  32 makefile = pyqtconfig.QtCoreModuleMakefile(
  33      configuration=config,
  34          build_file=build_file,
  35          installs=installs
  36      )
  37 
  38 
  39 
  40 
  41 makefile.LFLAGS.append("-L../c++")
  42 makefile.extra_libs = ["hello"]
  43 
  44 
  45 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:

   1 import hello
   2 
   3 a=hello.Hello()
   4 print a