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 – converting data between C++ and Python

Create a new class and call it QtPythonValue. Then, add the following code to it:

#include <Python.h> class QtPythonValue { public: QtPythonValue() { incRef(Py_None);} QtPythonValue(const QtPythonValue &other) { incRef(other.m_value); } QtPythonValue& operator=(const QtPythonValue &other) { if(m_value == other.m_value) return *this; decRef(); incRef(other.m_value); return *this; } QtPythonValue(int val) { m_value = PyLong_FromLong(val); } QtPythonValue(const QString &str) { m_value = PyUnicode_FromString(qPrintable(str)); } ~QtPythonValue() { decRef(); } int toInt() const { return PyLong_Check(m_value) ? PyLong_AsLong(m_value) : 0; } QString toString() const { return PyUnicode_Check(m_value) ...

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