Skip to content

Commit 83b6943

Browse files
author
ezust
committed
Added an example QFileImportInterface which doesn't work yet!
git-svn-id: http://svn.code.sf.net/p/pythonqt/code/trunk@42 ea8d5007-eb21-0410-b261-ccb3ea6e24a9
1 parent 52264f7 commit 83b6943

File tree

4 files changed

+82
-4
lines changed

4 files changed

+82
-4
lines changed

examples/CPPPyWrapperExample/CPPPyWrapperExample.cpp

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include <PythonQt.h>
22
#include <QtGui>
3+
#include "QFileImportInterface.h"
4+
35
int main (int argc, char* argv[]) {
46
QApplication app(argc, argv);
57
PythonQt::init();
68
PythonQtObjectPtr mainModule = PythonQt::self()->getMainModule();
9+
mainModule.evalScript(QString("import sys\n"));
710
Q_ASSERT(!mainModule.isNull());
811
{
912
// evaluate a python file embedded in executable as resource:
@@ -17,10 +20,22 @@ int main (int argc, char* argv[]) {
1720
// tag goes out of scope, reference count decremented.
1821
}
1922
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"; */
2037
{ // alternative using import and loading it as a real module from sys.path
2138
// import sys first
22-
mainModule.evalScript(QString("import sys\n"));
23-
// append the current directory to the sys.path
2439
mainModule.evalScript(QString("sys.path.append('%1')\n").arg(QDir::currentPath()));
2540
mainModule.evalScript("import eyed3tagger\n");
2641
PythonQtObjectPtr tag = mainModule.evalScript("eyed3tagger.EyeD3Tagger()\n", Py_eval_input);
@@ -29,7 +44,6 @@ int main (int argc, char* argv[]) {
2944
QVariant fn = tag.call("fileName", QVariantList());
3045
Q_ASSERT(fn.toString() == QString("t.mp3"));
3146
}
32-
3347
qDebug() << "finished";
3448
return 0;
3549
}

examples/CPPPyWrapperExample/CPPPyWrapperExample.pro

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ CONFIG += debug
22
VPATH +=
33
INCLUDEPATH += . $$(PYTHONQT_ROOT)/src /usr/include/python2.5
44

5-
SOURCES += CPPPyWrapperExample.cpp
5+
SOURCES += CPPPyWrapperExample.cpp QFileImportInterface.cpp
6+
HEADERS += QFileImportInterface.h
7+
68

79
LIBS += -L$$(PYTHONQT_ROOT)/lib -lPythonQt -lutil
810

Original file line numberDiff line numberDiff line change
@@ -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+
}
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)