Results 1 to 6 of 6

Thread: Use QObject with QtScript

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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
  •  
Qt is a trademark of The Qt Company.