Six simple steps to embed Python into your Qt C++ project.
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/
#include "embpyqt/embeddedpyqt.h"
...
EmbeddedPyQt embpyqt = new EmbeddedPyQt();
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.
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.
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