Results 1 to 10 of 10

Thread: QtScript Questions

  1. #1
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QtScript Questions

    I am embarking on some QtScript stuff and have some questions:

    1) Can I include files? I seem to be able to use eval() however if I provide a function include(filename) {..} in the script I run into the problem that the functions will only be accessible from within the function and not added to the root context.

    2) I am trying to use animations, and spefically QPropertyAnimation(item, property, parent) however my custom properties do not seem to work. I am using a JS-defined class, with properties as this.opacity, and passing "opacity" in the constructor. What does it take to have QPropertyAnimation be able to use JS-defined properties?

    Thanks.

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

    Default Re: QtScript Questions

    Quote Originally Posted by Scorp2us View Post
    I am embarking on some QtScript stuff and have some questions:

    1) Can I include files? I seem to be able to use eval() however if I provide a function include(filename) {..} in the script I run into the problem that the functions will only be accessible from within the function and not added to the root context.
    Can we see the exact code?
    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
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtScript Questions

    Qt Code:
    1. qs> eval("function f(i){return i*3;}");
    2. qs> b=f(5);
    3. 15
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. qs> function a(i) { eval("function F(i) {return i*3;}"); }
    2. qs> a
    3. function a(i) {
    4. eval("function F(i) {return i*3;}");
    5. }
    6. qs> a();
    7. qs> F(6);
    8. ReferenceError: F is not defined
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. qs> function b() { eval("function F(i) {return i*3;}"); return F(4); }
    2. qs> b()
    3. 12
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: QtScript Questions

    Your eval() (in the a() function) creates its own scope, whatever you assign there will not go to the global scope.
    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. #5
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtScript Questions

    Quote Originally Posted by wysota View Post
    Your eval() (in the a() function) creates its own scope, whatever you assign there will not go to the global scope.
    Exactly! (I knew this). But is there a way around that? Like a special context that I can specify (like a Python Module __main__) ?

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

    Default Re: QtScript Questions

    I think you have to look on JavaScript forums for that but the question is why are you trying to define a global function from eval(). You can do that from C++ code if you want... Also you can name the global object somehow in the C++ code and then you can access it from JavaScript code and assign the function to it - it should then be globally available. You can also return the function from the eval code and assign it to the global namespace in the parent (also JavaScript) code.
    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.


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

    Scorp2us (29th June 2009)

  8. #7
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtScript Questions

    I was hoping to do all this in JS, but it looks like I can't.

    Thanks.

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

    Default Re: QtScript Questions

    If you return a function object from the eval() call then you can do everything in JavaScript.

    javascript Code:
    1. F = eval("return function(){ return 7; }");
    2. x = F();
    To copy to clipboard, switch view to plain text mode 
    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.


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

    Scorp2us (29th June 2009)

  11. #9
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QtScript Questions

    Yeah, but that eval is in the global context.

    I still can't have a function defining a global function.

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

    Default Re: QtScript Questions

    Your a() function can return the result of eval().

    javascript Code:
    1. function a(){ return eval("return function(){ return 7 }") }
    2. F = a()
    To copy to clipboard, switch view to plain text mode 

    But I would try something like this:
    javascript Code:
    1. globalObj = this // global context
    2. function a() { eval("globalObj.F = function(){ return 7; }") }
    3. a()
    4. F()
    To copy to clipboard, switch view to plain text mode 
    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. Who gets questions answered and who doesn't?
    By gjshannon in forum General Discussion
    Replies: 15
    Last Post: 19th March 2009, 21:02
  2. QtScript Binding problem with QWidget
    By zack in forum Qt Programming
    Replies: 3
    Last Post: 17th February 2009, 09:38
  3. Memory management questions (im new to Qt)
    By scarvenger in forum Qt Programming
    Replies: 2
    Last Post: 6th May 2007, 07:41
  4. Qt related questions and thoughts about getting job
    By AlexKiriukha in forum General Discussion
    Replies: 4
    Last Post: 26th January 2006, 12:25

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.