Time for action – Calling functions and returning values

The next task is to provide ways to invoke Python functions and return values from scripts. Let's start by providing a richer run() API. Implement the following method in the QtPython class:

QtPythonValue QtPython::run(const QString &program,
    const QtPythonValue &globals, const QtPythonValue &locals)
{
    PyObject *retVal = PyRun_String(qPrintable(program),
        Py_file_input, globals.m_value, locals.m_value);
    return QtPythonValue(retVal);
} 

We'll also need a functionality to import Python modules. Add the following methods to the class:

QtPythonValue QtPython::import(const QString &name) const { return QtPythonValue(PyImport_ImportModule(qPrintable(name))); } QtPythonValue QtPython::addModule(const ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.