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