1. Henning Schröder
  2. embeddedpyqt

Source

embeddedpyqt / doc / integration.rst

How to add EmbeddedPyQt to your C++ project

Quickstart

Six simple steps to embed Python into your Qt C++ project.

  1. Add the embpyqt folder to your project.
  2. Add the embpyqt-files and Python to your build. For qmake do
INCLUDEPATH += -I./embpyqt
SOURCES += \
    embpyqt/embeddedpyqt.cpp \
    embpyqt/pythonize.cpp
HEADERS += \
    embpyqt/embeddedpyqt.h \
    embpyqt/pythonize.h
# Python
LIBS        += -lpython2.7 -L/usr/lib/
INCLUDEPATH += /usr/include/python2.7/
  1. Create an EmbeddedPyQt instance in your project:
#include "embpyqt/embeddedpyqt.h"
...
EmbeddedPyQt embpyqt = new EmbeddedPyQt();
  1. Register classes and instances to embpyqt:
embpyqt->registerObject(mainWindow);
embpyqt->registerMetaObject(Test::staticMetaObject);

The instances will be known by their object name. To register classes you have to pass the staticMetaObject attribute.

If an instance is a child object of a already registered instance (like embpyqt) you do not have to register it manually. It will be accessible as an attribute. The same applies to instance properties.

  1. After that call the init script:
embpyqt->init("embpyqt/python/initembpyqt.py");

Make sure that the python folder is copied to your installation target by your build system and that the path is correct.

Demo

The embeddedpyqt package already comes with a simple example. The root folder contains a small C++ application

  • main.cpp creates the QApplication and main window instance
  • mainwindow.{cpp,h,ui} sets up the user interface and integrates EmbeddedPyQt as show above
  • test.{cpp.h} is a demo class which has to methods (test and calc)
  • embeddedpyqt.pro is used by qmake to create a Makefile