Results 1 to 2 of 2

Thread: Can I include a script from script?

  1. #1
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Can I include a script from script?

    I try to include a script from script

    Qt Code:
    1. class Script: public QObject
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. Script();
    7. public slots:
    8. QWidget* loadGUI(QString file);
    9. void load(QString file); //using engine->evaluate(file.tostring) to load js file
    10. };
    To copy to clipboard, switch view to plain text mode 

    main.js
    Qt Code:
    1. SCRIPT.load('calculator.js'); //class Calculator is define in calculator.js
    2. gg= SCRIPT.loadGUI('calculator.ui');
    3. new Calculator(gg);
    4. gg.show();
    To copy to clipboard, switch view to plain text mode 

    But I got the error : Calculator is not defined

    If I move some codes from main.js to calculator.ui

    main.js
    Qt Code:
    1. gg= SCRIPT.loadGUI('calculator.ui');
    2. SCRIPT.load('calculator.js');
    To copy to clipboard, switch view to plain text mode 
    calculator.js
    Qt Code:
    1. ........
    2. //add this code
    3. new Calculator(gg);
    4. gg.show();
    To copy to clipboard, switch view to plain text mode 
    It work very well
    Last edited by yycking; 22nd April 2009 at 06:29.

  2. #2
    Join Date
    Mar 2009
    Posts
    14
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Can I include a script from script?

    I got it.
    Must set ScriptContext before using evaluate

    Qt Code:
    1. void Script::load(QString path)
    2. {
    3. QFile scriptFile(path);
    4.  
    5. //check file is exited or not
    6. if(!scriptFile.open(QIODevice::ReadOnly))
    7. {
    8. return;
    9. }
    10.  
    11. //load file
    12. QTextStream stream(&scriptFile);
    13. QString s=stream.readAll();
    14. scriptFile.close();
    15.  
    16. //set ScriptContext
    17. QScriptContext *context = engine->currentContext();
    18. QScriptContext *parent=context->parentContext();
    19. if(parent!=0)
    20. {
    21. context->setActivationObject(context->parentContext()->activationObject());
    22. context->setThisObject(context->parentContext()->thisObject());
    23. }
    24.  
    25. //exctue script
    26. QScriptValue result = engine->evaluate(s);
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QApplication: no such file or directory
    By jochen_r in forum Newbie
    Replies: 13
    Last Post: 15th November 2008, 21:46
  2. Error: Using Multiple files
    By 3nc31 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 09:23
  3. Painting problem
    By Mel in forum Qt Programming
    Replies: 10
    Last Post: 8th May 2007, 21:38
  4. Can't create an object : initialisation problem ?
    By Nyphel in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2007, 09:07
  5. use button from another Window
    By raphaelf in forum Qt Programming
    Replies: 11
    Last Post: 2nd March 2006, 20:31

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.