O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

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 – 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() ...

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