O'Reilly logo

Game Programming Using Qt by Lorenz Haas, Witold Wysota

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required