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 QString &name) const { PyObject *retVal = PyImport_AddModule(qPrintable(name)); Py_INCREF(retVal); return QtPythonValue(retVal); } QtPythonValue QtPython::dictionary(const QtPythonValue &module) const { ...