[PyQt] SIP Wrapping of C++ Qt code for PyQt

Brandon King kingb at caltech.edu
Wed Apr 11 01:05:39 BST 2007


Hello,
    I am trying to evaluate SIP for use with wrapping a C++ Qt 
application for use within Python with PyQt. I have a very simple Qt 
Widget and want to wrap it, but without much success... When I run 
configure.py (see below), and then run 'make', everything compiles and I 
get a QtGui.so in the end. At first I got this error message:

"<type 'exceptions.ImportError'>: libquit_button.so.1: cannot open 
shared object file: No such file or directory"

Then when I added libquit_button.so.1 to the directory, I now get this 
error:

"<type 'exceptions.SystemError'>: dynamic module not initialized properly'.

Any idea what I am doing wrong? Is there any documentation showing how 
to wrap more than one Qt Widget for use with PyQt? Are there any other 
examples besides the SIP manual? Any other suggestions on getting 
started with SIP other than reading PyQt4 and PyKDE code?

-Brandon King



FYI, code used as test is below:

c++/quit_button.hpp:

#ifndef _MYWIDGET_H_
#define _MYWIDGET_H_

class QWidget;

class MyWidget : public QWidget
{
public:
    MyWidget(QWidget *parent = 0);
};

#endif



c++/quit_button.cpp:

#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QWidget>

#include "quit_button.hpp"

MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
    setFixedSize(200, 120);

    QPushButton *quit = new QPushButton(tr("Quit"), this);
    quit->setGeometry(62, 40, 75, 30);
    quit->setFont(QFont("Times", 18, QFont::Bold));

    connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
}



sip/quit_button.sip:

// Sip Wrapper for quit_button

%Module quit_button 0

%Include QtGui/QtGuimod.sip
//%Include QtCore/QtCoremod.sip


class MyWidget : QWidget
{
%TypeHeaderCode
#include "../c++/quit_button.hpp"
%End

public:
  MyWidget(QWidget *parent /TransferThis/);

private:
  MyWidget(const MyWidget &);

};


sip/configure.py:

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 = "quit_button.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, "quit_button.sip"]))

# We are going to install the SIP specification file for this module and
# its configuration module.
installs = []

installs.append(["quit_button.sip", os.path.join(config.default_sip_dir, "quit_button")])

installs.append(["quit_buttonconfig.py", config.default_mod_dir])

# 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.QtGuiModuleMakefile(
      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 = ["quit_button"]

# Generate the Makefile itself.
makefile.generate()






More information about the PyQt mailing list