This tutorial explains in 14 steps how to use pybind11
for creating a Python scripting engine for a Qt application.
It first explains how to create a module that can be imported in Python, continues by prosenting different aspects of running inside of a C++ (Qt) application a Python script that can access (and modify) the status of the main application.
Each step contains code that can be individually compiled, run, and – of course – modified.
You should probably go through each step in the order presented below, since the commands are (briefly) introduced only once.
There are three ways for getting pybind11:
pybind11-dev
and python3-pybind11
.The samples in this repositories use a pre-compiled version of pybind11. If you're not getting pybind11 from your distribution repositories:
mkdir build && cd build
cmake -D CMAKE_INSTALL_PREFIX=/the/prefix/where/you/install/pybind11 -D PYBIND11_TEST=OFF ..
make install
The pybind11 authors prefer adding pybind11 as a git sub-project: you can of course follow their instruction and adapt the CMakeLists.txt
files in this repository.
You need:
libpython3-dev
, python-cxx-dev
, ...)Our first example directly uses the compiler, all other examples will need a modern version of cmake
being installed (pybind11 needs cmake 3.4)
add/
: create a module providing a C++ add
function that can be add two numbers.add-cmake
: the first example with CMake (all further examples use CMake).classes/
: let Python use C++ classes.eval-file/
: C++ evaluates a Python script in an external files that modifies the state of a C++ variable.eval-set-object/
: Create two objects of type Foo
and pass them by value or by reference to a Python script (defined as a string in the C++ code)eval-set-object-module/
: Create a .so
module for the Foo
type, load it in the Python interpreter and let the Python script access it.eval-file-pyqt5/
: The C++ code runs an external Python script that shows a PyQt5 alert.eval-file-pyqt5-set-value/
: Setting a C++ value from a PyQt5 input dialog.exec()
call. The visibility of the global and local variables works in the same way: https://gist.github.com/dean0x7d/df5ce97e4a1a05be4d56d1378726ff92CMakeLists.txt
files to require Python 3.7. (eval-set-object-module
does somehow require it)Document all directories:
open questions:
QApplication()
in eval-file-pyqt5/python/pyqt5.py