Time for action – Writing a Qt wrapper for embedding Python

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:

class QtPython : public QObject {
    Q_OBJECT
public:
    QtPython(QObject *parent = 0);
    ~QtPython();
    void run(const QString &program);

private:
    QVector<wchar_t> programNameBuffer;
};

The implementation file should look like this:

#include <Python.h> //... QtPython::QtPython(QObject *parent) : QObject(parent) { QStringList args = qApp->arguments(); if (args.count() > 0) { programNameBuffer.resize(args[0].count()); args[0].toWCharArray(programNameBuffer.data()); Py_SetProgramName(programNameBuffer.data()); } Py_InitializeEx(0); } QtPython::~QtPython() ...

Get Game Programming using Qt 5 Beginner's Guide - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.