Results 1 to 6 of 6

Thread: QScript Date/Array/Object, can't find variable ?!

  1. #1
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    84
    Thanks
    7
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QScript Date/Array/Object, can't find variable ?!

    hi,

    when a qscript is evaluated standard objects cannot be found:

    for instance:
    Qt Code:
    1. function A()
    2. {
    3. // var cdt = new Object();
    4. // var cdt = new Array(2);
    5.  
    6. var dt = new Date();
    7.  
    8. var cdt = [2];
    9. cdt[0] = '2012-02-03';
    10.  
    11. return cdt;
    To copy to clipboard, switch view to plain text mode 

    }

    what do i have to do that a script can use those types?
    best regards,
    jh

    here's the qscript error:

    uncaught exception at line 12
    : ReferenceError: Can't find variable: Date

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScript Date/Array/Object, can't find variable ?!

    You must have messed something up in your code. Date works fine for me.

    Qt Code:
    1. #include <QScriptEngine>
    2. #include <QtCore>
    3.  
    4. int main(int argc, char **argv) {
    5. QCoreApplication app(argc, argv);
    6. QScriptEngine engine;
    7. qDebug() << engine.evaluate("new Date()").toString();
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Result: "Mon Apr 23 2012 13:08:22 GMT+0200 (CEST)"
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    84
    Thanks
    7
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QScript Date/Array/Object, can't find variable ?!

    hi,

    thanx for your reply. i found out that 'Date' is not known if i set the 'this' object. otherwise it works.

    i do this:

    'ObjectWrapper' is a QObject with signals/slots.

    Qt Code:
    1. ObjectWrapper* qobj = new ObjectWrapper(0,ge);
    2. QScriptValue sobj = _myEngine->newQObject(qobj);
    3. _myEngine->currentContext()->setThisObject( sobj );
    To copy to clipboard, switch view to plain text mode 

    regards,
    jh

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScript Date/Array/Object, can't find variable ?!

    Date is a property of the Global Object. By the way, if the above code is the actual code you have then it's probably wrong. I'm assuming the current context of your engine when this code is executed is the global context. In this situation you shouldn't be setting a "this" object. Instead push a new context on the stack and then you'll be able to set "this" object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    jh (23rd April 2012)

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    84
    Thanks
    7
    Thanked 2 Times in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QScript Date/Array/Object, can't find variable ?!

    i pushed/poped a new context to the engine and now it works fine.
    thanx a lot!
    regards,
    jh

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QScript Date/Array/Object, can't find variable ?!

    One more thing, for completeness. There are two most commonly used ways of exposing an object to scripting environment. One, more often used, is to expose it as a property of the global object. Then the object is visible throughout the whole life of the engine (or to be more precise of the engine's global object). An alternative is to expose it as a property of a context of the engine. Then the object can be accessed as long as the context is the active context of the engine. What you did is a third way -- you exposed the object as the context itself. Consider using one of the more common methods to avoid naming conflicts.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 3
    Last Post: 25th February 2012, 22:25
  2. save variable size hex string into uchar array
    By amika in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2011, 22:35
  3. OpenGL Vertex Buffer Array/Object
    By Denarius in forum Qt Programming
    Replies: 1
    Last Post: 8th April 2010, 08:59
  4. Replies: 1
    Last Post: 19th February 2010, 11:02

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.