January 2016
Beginner
512 pages
12h 35m
English
As the first task, we will implement the last program using an object-oriented API. Create a new console project and add the following class to it:
#include <Python.h>
#include <QObject>
#include <QString>
class QtPython : public QObject {
Q_OBJECT
public:
QtPython(const char *progName, QObject *parent = 0) : QObject(parent) {
if(progName != 0) {
wchar_t buf[strlen(progName+1)];
mbstowcs(buf, progName, strlen(progName));
Py_SetProgramName(buf);
}
Py_InitializeEx(0);
}
~QtPython() { Py_Finalize(); }
void run(const QString &program) {
PyRun_SimpleString(qPrintable(program));
}
};Then, add a
main() function as shown in the following snippet:
#include "qtpython.h" int main(int argc, char **argv) ...
Read now
Unlock full access