How to add EmbeddedPyQt to your C++ project
Six simple steps to embed Python into your Qt C++ project.
- Add the embpyqt folder to your project.
- 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/
- Create an EmbeddedPyQt instance in your project:
#include "embpyqt/embeddedpyqt.h" ... EmbeddedPyQt embpyqt = new EmbeddedPyQt();
- Register classes and instances to embpyqt:
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.
- Call the init script:
Make sure that the python folder is copied to your installation target by your build system and that the path is correct.
- Now you can call Python from C++ by using the execute method:
embpyqt->execute("embpyqt_console.Visible = True", true);
Python can connect to existing C++ signals and is able to modify the user interface.