PDA

View Full Version : Use QObject with QtScript



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.

Urthas
16th August 2010, 17:44
Sorry, er...what's "var" exactly? Does Qt think it's a class name and is therefore looking for var::exist()?

Ben1
17th August 2010, 08:14
var is the declaration of a variable in QtScript (I think)

The code :
var file = File("toto.js");
file.exist();
file.readAll();
Math.Random();
is my QtScript code (like ECMA). I may mistake when I wrote this.

Note : I add Q_INVOKABLE before all declaration of method in the .h file, nothing change.

asvil
17th August 2010, 08:59
May be you have null value in qt script.


print(typeof file)

Ben1
17th August 2010, 09:32
I wrote this :
var file = File("toto.js");
print(typeof file);

and in the cpp file:
QScriptValue loc_value = m_engine->evaluate(loc_currentScenario->GetProgram(), "fichier.txt");
qDebug(QString(loc_value.toString()).toLatin1());

the result is : undefined

Does it mean that the var file is null ? (I check if the return of the register méthod is null, and it's null)

Ben1
17th August 2010, 10:00
Ok, my mistake seems to be here :

return file->exist() ? engine->newQObject(file) : engine->nullValue();

replace by :

return engine->newQObject(file) ;

And it seems work correctly