Ben1
16th August 2010, 15:37
Hi everyboby,
I read the doc about how to use QObject with QtScript but I must mistake.
I re implement QFile
class FileRegister
{
public:
static void Register(QScriptEngine *engine);
};
class File : public QObject
{
Q_OBJECT
public:
File(QString par_filePath, QObject *parent = 0);
File(const File &);
~File();
bool exist() { return file->exists(); }
QString getName() { return file->fileName(); }
QString getPath() { return fileInfo.path(); }
int getSize() { return (int) file->size(); }
bool isReadOnly() { return !fileInfo.isWritable(); }
bool isHidden() { return fileInfo.isHidden(); }
QDateTime getLastModified() { return fileInfo.lastModified(); }
QDateTime getCreation() { return fileInfo.created(); }
QString readAll();
QString read(int length);
bool write(QString data);
bool resize(int length, QString text = "");
bool append(QString text);
bool compareContent(File file);
bool rename(QString newName);
bool copy(QString newPath);
bool move(QString newPath);
bool deleteFile();
File operator =(File file);
private:
QFile *file;
QFileInfo fileInfo;
};
QScriptValue createFile(QScriptContext *context, QScriptEngine *engine)
{
if(context->argumentCount() != 1 || !context->argument(0).isString())
{
return engine->nullValue();
}
File *file = new File(context->argument(0).toString(), engine);
return file->exist() ? engine->newQObject(file) : engine->nullValue();
}
void FileRegister::Register(QScriptEngine *engine)
{
engine->globalObject().setProperty("File", engine->newFunction(createFile));
}
And I call
QScriptEngine *engine = new QScriptEngine(parent);
FileRegister::Register(loc_engine);
but when I do engine->evaluate("test.txt") where test.txt contains this code
var file = File("toto.js");
file.exist();
file.readAll();
Math.Random();
I'have got this problem
ReferenceError: exist is not defined : line => 2
Thank you in advance.
I read the doc about how to use QObject with QtScript but I must mistake.
I re implement QFile
class FileRegister
{
public:
static void Register(QScriptEngine *engine);
};
class File : public QObject
{
Q_OBJECT
public:
File(QString par_filePath, QObject *parent = 0);
File(const File &);
~File();
bool exist() { return file->exists(); }
QString getName() { return file->fileName(); }
QString getPath() { return fileInfo.path(); }
int getSize() { return (int) file->size(); }
bool isReadOnly() { return !fileInfo.isWritable(); }
bool isHidden() { return fileInfo.isHidden(); }
QDateTime getLastModified() { return fileInfo.lastModified(); }
QDateTime getCreation() { return fileInfo.created(); }
QString readAll();
QString read(int length);
bool write(QString data);
bool resize(int length, QString text = "");
bool append(QString text);
bool compareContent(File file);
bool rename(QString newName);
bool copy(QString newPath);
bool move(QString newPath);
bool deleteFile();
File operator =(File file);
private:
QFile *file;
QFileInfo fileInfo;
};
QScriptValue createFile(QScriptContext *context, QScriptEngine *engine)
{
if(context->argumentCount() != 1 || !context->argument(0).isString())
{
return engine->nullValue();
}
File *file = new File(context->argument(0).toString(), engine);
return file->exist() ? engine->newQObject(file) : engine->nullValue();
}
void FileRegister::Register(QScriptEngine *engine)
{
engine->globalObject().setProperty("File", engine->newFunction(createFile));
}
And I call
QScriptEngine *engine = new QScriptEngine(parent);
FileRegister::Register(loc_engine);
but when I do engine->evaluate("test.txt") where test.txt contains this code
var file = File("toto.js");
file.exist();
file.readAll();
Math.Random();
I'have got this problem
ReferenceError: exist is not defined : line => 2
Thank you in advance.