CPPPyWrapperExample.cpp
45 lines
| 2.0 KiB
| text/x-c
|
CppLexer
ezust
|
r2 | #include <PythonQt.h> | ||
#include <QtGui> | ||||
ezust
|
r7 | |||
ezust
|
r2 | int main (int argc, char* argv[]) { | ||
QApplication app(argc, argv); | ||||
PythonQt::init(); | ||||
PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule(); | ||||
ezust
|
r7 | mainModule.evalScript(QString("import sys\n")); | ||
ezust
|
r2 | Q_ASSERT(!mainModule.isNull()); | ||
florianlink
|
r10 | { | ||
ezust
|
r2 | // evaluate a python file embedded in executable as resource: | ||
mainModule.evalFile(":eyed3tagger.py"); | ||||
ezust
|
r6 | // create an object, hold onto its reference | ||
ezust
|
r2 | 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")); | ||||
ezust
|
r6 | // tag goes out of scope, reference count decremented. | ||
ezust
|
r2 | } | ||
florianlink
|
r10 | { | ||
ezust
|
r7 | // Allow the python system path to recognize QFile paths in the sys.path | ||
florianlink
|
r8 | PythonQt::self()->setImporter(NULL); | ||
ezust
|
r7 | // 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")); | ||||
florianlink
|
r10 | } | ||
ezust
|
r2 | { // 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; | ||||
} | ||||