Results 1 to 1 of 1

Thread: QSA: loading scripts at runtime

  1. #1
    Join Date
    Jan 2006
    Posts
    132
    Thanked 16 Times in 16 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSA: loading scripts at runtime

    I came across the problem that there is no convenient way to load scripts at runtime. What I first tried was to do it this way:

    Qt Code:
    1. eval(File.read('foo.qs'));
    2. foo();
    To copy to clipboard, switch view to plain text mode 

    foo.qs:
    Qt Code:
    1. function foo()
    2. {
    3. debug('hello from foo()');
    4. }
    To copy to clipboard, switch view to plain text mode 

    Basicly this would work, unfortunately code in eval is not evaluated at global scope, so foo() is unknown after eval() and thus cannot be used. The solution tor the problem is to load the file in C++ and call QSInterpreter::evaluate to process the file. Since there is no convenient support for doing this, I have created a objectfactory as solution (see attachment).

    Usage:

    To use the script class add it to your interpreter with:

    Qt Code:
    1. #include "qsscriptfactory.h"
    2. ...
    3. interpreter->addObjectFactory(new QSScriptFactory);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Then you can use it in your scripts as:

    Script.load(code, scriptname, multiple = false);
    Load script code from a string in 'code'.
    'scriptname' is mandatory and must be unique for distinct scripts.
    'multiple' false = ignore attemts to load same script twice. true = allow multiple loading of same script.

    Script.loadFile(filename, multiple = false);
    Load script from file 'filename'.
    'multiple' false = ignore attemts to load same script twice. true = allow multiple loading of same script.

    Example:

    Script.loadFile('foo.qs'); // load/evaluate script in file foo.qs

    Enjoy!
    Attached Files Attached Files

Similar Threads

  1. QSA scripts: how to wait for events?
    By olberg in forum Qt Programming
    Replies: 2
    Last Post: 17th May 2006, 14:56

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.