@@ -0,0 +1,42 | |||||
|
1 | #include <QFile> | |||
|
2 | #include <QFileInfo> | |||
|
3 | ||||
|
4 | #include <PythonQt.h> | |||
|
5 | #include "QFileImportInterface.h" | |||
|
6 | ||||
|
7 | QFileImportInterface::QFileImportInterface() { | |||
|
8 | m_oldInterface = PythonQt::importInterface(); | |||
|
9 | PythonQt::self()->setImporter(this); | |||
|
10 | } | |||
|
11 | ||||
|
12 | QFileImportInterface::~QFileImportInterface() { | |||
|
13 | PythonQt::self()->setImporter(m_oldInterface); | |||
|
14 | } | |||
|
15 | ||||
|
16 | QByteArray QFileImportInterface::readFileAsBytes (const QString &filename) { | |||
|
17 | qDebug() << "readFileAsBytes: " << filename; | |||
|
18 | QFile f(filename); | |||
|
19 | return f.readAll(); | |||
|
20 | } | |||
|
21 | ||||
|
22 | QByteArray QFileImportInterface::readSourceFile (const QString &filename, bool &ok) { | |||
|
23 | QFile f(filename); | |||
|
24 | if (!exists(filename)) { | |||
|
25 | ok = false; | |||
|
26 | return QByteArray(); | |||
|
27 | } | |||
|
28 | else { | |||
|
29 | ok = true; | |||
|
30 | return readFileAsBytes(filename); | |||
|
31 | } | |||
|
32 | } | |||
|
33 | ||||
|
34 | bool QFileImportInterface::exists (const QString &filename) { | |||
|
35 | QFile f(filename); | |||
|
36 | return f.exists(); | |||
|
37 | } | |||
|
38 | ||||
|
39 | QDateTime QFileImportInterface::lastModifiedDate (const QString &filename) { | |||
|
40 | QFileInfo fi(filename); | |||
|
41 | return fi.lastModified(); | |||
|
42 | } |
@@ -0,0 +1,20 | |||||
|
1 | #ifndef QFILEIMPORTINTERFACE_H | |||
|
2 | #define QFILEIMPORTINTERFACE_H | |||
|
3 | ||||
|
4 | #include <PythonQtImportFileInterface.h> | |||
|
5 | ||||
|
6 | /** Under Construction : This does not work yet. | |||
|
7 | */ | |||
|
8 | class QFileImportInterface : public PythonQtImportFileInterface { | |||
|
9 | public: | |||
|
10 | QFileImportInterface(); | |||
|
11 | ~QFileImportInterface(); | |||
|
12 | QByteArray readFileAsBytes (const QString &filename); | |||
|
13 | QByteArray readSourceFile (const QString &filename, bool &ok); | |||
|
14 | bool exists (const QString &filename); | |||
|
15 | QDateTime lastModifiedDate (const QString &filename); | |||
|
16 | private: | |||
|
17 | PythonQtImportFileInterface *m_oldInterface; | |||
|
18 | }; | |||
|
19 | ||||
|
20 | #endif // #ifndef QFILEIMPORTINTERFACE_H |
@@ -1,35 +1,49 | |||||
1 | #include <PythonQt.h> |
|
1 | #include <PythonQt.h> | |
2 | #include <QtGui> |
|
2 | #include <QtGui> | |
|
3 | #include "QFileImportInterface.h" | |||
|
4 | ||||
3 | int main (int argc, char* argv[]) { |
|
5 | int main (int argc, char* argv[]) { | |
4 | QApplication app(argc, argv); |
|
6 | QApplication app(argc, argv); | |
5 | PythonQt::init(); |
|
7 | PythonQt::init(); | |
6 | PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule(); |
|
8 | PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule(); | |
|
9 | mainModule.evalScript(QString("import sys\n")); | |||
7 | Q_ASSERT(!mainModule.isNull()); |
|
10 | Q_ASSERT(!mainModule.isNull()); | |
8 | { |
|
11 | { | |
9 | // evaluate a python file embedded in executable as resource: |
|
12 | // evaluate a python file embedded in executable as resource: | |
10 | mainModule.evalFile(":eyed3tagger.py"); |
|
13 | mainModule.evalFile(":eyed3tagger.py"); | |
11 | // create an object, hold onto its reference |
|
14 | // create an object, hold onto its reference | |
12 | PythonQtObjectPtr tag = mainModule.evalScript("EyeD3Tagger()\n", Py_eval_input); |
|
15 | PythonQtObjectPtr tag = mainModule.evalScript("EyeD3Tagger()\n", Py_eval_input); | |
13 | Q_ASSERT(!tag.isNull()); |
|
16 | Q_ASSERT(!tag.isNull()); | |
14 | tag.call("setFileName", QVariantList() << "t.mp3"); |
|
17 | tag.call("setFileName", QVariantList() << "t.mp3"); | |
15 | QVariant fn = tag.call("fileName", QVariantList()); |
|
18 | QVariant fn = tag.call("fileName", QVariantList()); | |
16 | Q_ASSERT(fn.toString() == QString("t.mp3")); |
|
19 | Q_ASSERT(fn.toString() == QString("t.mp3")); | |
17 | // tag goes out of scope, reference count decremented. |
|
20 | // tag goes out of scope, reference count decremented. | |
18 | } |
|
21 | } | |
19 | qDebug() << "test1"; |
|
22 | qDebug() << "test1"; | |
|
23 | /* | |||
|
24 | { | |||
|
25 | // Allow the python system path to recognize QFile paths in the sys.path | |||
|
26 | QFileImportInterface qfii; | |||
|
27 | // append the Qt resource root directory to the sys.path | |||
|
28 | mainModule.evalScript("sys.path.append(':')\n"); | |||
|
29 | mainModule.evalScript("import eyed3tagger\n"); | |||
|
30 | PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input); | |||
|
31 | Q_ASSERT(!tag.isNull()); | |||
|
32 | tag.call("setFileName", QVariantList() << "t.mp3"); | |||
|
33 | QVariant fn = tag.call("fileName", QVariantList()); | |||
|
34 | Q_ASSERT(fn.toString() == QString("t.mp3")); | |||
|
35 | } | |||
|
36 | qDebug() << "test2"; */ | |||
20 | { // alternative using import and loading it as a real module from sys.path |
|
37 | { // alternative using import and loading it as a real module from sys.path | |
21 | // import sys first |
|
38 | // import sys first | |
22 | mainModule.evalScript(QString("import sys\n")); |
|
|||
23 | // append the current directory to the sys.path |
|
|||
24 | mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath())); |
|
39 | mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath())); | |
25 | mainModule.evalScript("import eyed3tagger\n"); |
|
40 | mainModule.evalScript("import eyed3tagger\n"); | |
26 | PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input); |
|
41 | PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input); | |
27 | Q_ASSERT(!tag.isNull()); |
|
42 | Q_ASSERT(!tag.isNull()); | |
28 | tag.call("setFileName", QVariantList() << "t.mp3"); |
|
43 | tag.call("setFileName", QVariantList() << "t.mp3"); | |
29 | QVariant fn = tag.call("fileName", QVariantList()); |
|
44 | QVariant fn = tag.call("fileName", QVariantList()); | |
30 | Q_ASSERT(fn.toString() == QString("t.mp3")); |
|
45 | Q_ASSERT(fn.toString() == QString("t.mp3")); | |
31 | } |
|
46 | } | |
32 |
|
||||
33 | qDebug() << "finished"; |
|
47 | qDebug() << "finished"; | |
34 | return 0; |
|
48 | return 0; | |
35 | } |
|
49 | } |
@@ -1,9 +1,11 | |||||
1 | CONFIG += debug |
|
1 | CONFIG += debug | |
2 | VPATH += |
|
2 | VPATH += | |
3 | INCLUDEPATH += . $$(PYTHONQT_ROOT)/src /usr/include/python2.5 |
|
3 | INCLUDEPATH += . $$(PYTHONQT_ROOT)/src /usr/include/python2.5 | |
4 |
|
4 | |||
5 | SOURCES += CPPPyWrapperExample.cpp |
|
5 | SOURCES += CPPPyWrapperExample.cpp QFileImportInterface.cpp | |
|
6 | HEADERS += QFileImportInterface.h | |||
|
7 | ||||
6 |
|
8 | |||
7 | LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt -lutil |
|
9 | LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt -lutil | |
8 |
|
10 | |||
9 | RESOURCES += CPPPyWrapperExample.qrc |
|
11 | RESOURCES += CPPPyWrapperExample.qrc |
General Comments 0
You need to be logged in to leave comments.
Login now