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) ? QString::fromUtf8(PyUnicode_AsUTF8(m_value)) : QString();
}
bool isNone() const { return m_value == Py_None; }
private:
QtPythonValue(PyObject *ptr) { m_value...