forked from MeVisLab/pythonqt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPPPyWrapperExample.cpp
More file actions
47 lines (46 loc) · 1.91 KB
/
CPPPyWrapperExample.cpp
File metadata and controls
47 lines (46 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <PythonQt.h>
#include <QtGui>
#include <QApplication>
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
PythonQt::init();
PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule();
mainModule.evalScript(QString("import sys\n"));
Q_ASSERT(!mainModule.isNull());
{
// evaluate a python file embedded in executable as resource:
mainModule.evalFile(":eyed3tagger.py");
// create an object, hold onto its reference
PythonQtObjectPtr tag = mainModule.evalScript("EyeD3Tagger()\n", Py_eval_input);
Q_ASSERT(!tag.isNull());
tag.call("setFileName", QVariantList() << "t.mp3");
QVariant fn = tag.call("fileName", QVariantList());
Q_ASSERT(fn.toString() == QString("t.mp3"));
// tag goes out of scope, reference count decremented.
}
{
// Allow the python system path to recognize QFile paths in the sys.path
PythonQt::self()->setImporter(NULL);
// append the Qt resource root directory to the sys.path
mainModule.evalScript("sys.path.append(':')\n");
mainModule.evalScript("import eyed3tagger\n");
PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input);
Q_ASSERT(!tag.isNull());
tag.call("setFileName", QVariantList() << "t.mp3");
QVariant fn = tag.call("fileName", QVariantList());
Q_ASSERT(fn.toString() == QString("t.mp3"));
}
{ // alternative using import and loading it as a real module from sys.path
// import sys first
mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath()));
mainModule.evalScript("import eyed3tagger\n");
PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input);
Q_ASSERT(!tag.isNull());
tag.call("setFileName", QVariantList() << "t.mp3");
QVariant fn = tag.call("fileName", QVariantList());
Q_ASSERT(fn.toString() == QString("t.mp3"));
}
qDebug() << "finished";
return 0;
}