Results 1 to 6 of 6

Thread: Use QObject with QtScript

  1. #1
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Use QObject with QtScript

    Hi everyboby,

    I read the doc about how to use QObject with QtScript but I must mistake.

    I re implement QFile

    Qt Code:
    1. class FileRegister
    2. {
    3. public:
    4. static void Register(QScriptEngine *engine);
    5. };
    6.  
    7. class File : public QObject
    8. {
    9. Q_OBJECT
    10.  
    11. public:
    12. File(QString par_filePath, QObject *parent = 0);
    13. File(const File &);
    14. ~File();
    15.  
    16. bool exist() { return file->exists(); }
    17. QString getName() { return file->fileName(); }
    18. QString getPath() { return fileInfo.path(); }
    19. int getSize() { return (int) file->size(); }
    20. bool isReadOnly() { return !fileInfo.isWritable(); }
    21. bool isHidden() { return fileInfo.isHidden(); }
    22. QDateTime getLastModified() { return fileInfo.lastModified(); }
    23. QDateTime getCreation() { return fileInfo.created(); }
    24.  
    25. QString readAll();
    26. QString read(int length);
    27. bool write(QString data);
    28. bool resize(int length, QString text = "");
    29. bool append(QString text);
    30.  
    31. bool compareContent(File file);
    32.  
    33. bool rename(QString newName);
    34. bool copy(QString newPath);
    35. bool move(QString newPath);
    36. bool deleteFile();
    37.  
    38. File operator =(File file);
    39.  
    40. private:
    41. QFile *file;
    42. QFileInfo fileInfo;
    43. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QScriptValue createFile(QScriptContext *context, QScriptEngine *engine)
    2. {
    3. if(context->argumentCount() != 1 || !context->argument(0).isString())
    4. {
    5. return engine->nullValue();
    6. }
    7.  
    8. File *file = new File(context->argument(0).toString(), engine);
    9.  
    10. return file->exist() ? engine->newQObject(file) : engine->nullValue();
    11. }
    12.  
    13. void FileRegister::Register(QScriptEngine *engine)
    14. {
    15. engine->globalObject().setProperty("File", engine->newFunction(createFile));
    16. }
    To copy to clipboard, switch view to plain text mode 

    And I call

    Qt Code:
    1. QScriptEngine *engine = new QScriptEngine(parent);
    2.  
    3. FileRegister::Register(loc_engine);
    To copy to clipboard, switch view to plain text mode 

    but when I do engine->evaluate("test.txt") where test.txt contains this code

    Qt Code:
    1. var file = File("toto.js");
    2. file.exist();
    3. file.readAll();
    4. Math.Random();
    To copy to clipboard, switch view to plain text mode 

    I'have got this problem

    Qt Code:
    1. ReferenceError: exist is not defined : line => 2
    To copy to clipboard, switch view to plain text mode 

    Thank you in advance.
    Last edited by Ben1; 16th August 2010 at 15:44.

  2. #2
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default Re: Use QObject with QtScript

    Sorry, er...what's "var" exactly? Does Qt think it's a class name and is therefore looking for var::exist()?

  3. #3
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Use QObject with QtScript

    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.

  4. #4
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Use QObject with QtScript

    May be you have null value in qt script.
    Qt Code:
    1. print(typeof file)
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Use QObject with QtScript

    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)
    Last edited by Ben1; 17th August 2010 at 09:55.

  6. #6
    Join Date
    Jul 2010
    Location
    France
    Posts
    18
    Thanks
    2
    Qt products
    Qt4 Qt Jambi
    Platforms
    Unix/X11 Windows

    Default Re: Use QObject with QtScript

    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

Similar Threads

  1. Replies: 2
    Last Post: 18th June 2013, 10:31
  2. Replies: 2
    Last Post: 30th December 2010, 10:25
  3. qtscript
    By wookoon in forum Newbie
    Replies: 4
    Last Post: 4th July 2010, 11:53
  4. Replies: 0
    Last Post: 25th November 2009, 07:46
  5. Replies: 1
    Last Post: 31st October 2007, 14:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.