Results 1 to 3 of 3

Thread: How to get value from Script file?

  1. #1
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Smile How to get value from Script file?

    Hi.

    I have a problem about Qtscript.

    My project is a com port tool and my idea is: The main project loads a script file and returns commands to the C++ code, then the commands from script file are sent to the peripheral equipment. In this way I just need to change the script file if I want to send different commands each time.

    Now I did in this way:
    this is the C++ code to interpret script file:
    Qt Code:
    1. QScriptEngine engine;
    2. QString scriptFileName(":/myscriptfile.js");// Change the script file according to the requirement.
    3. QFile file(scriptFileName);
    4. file.open(QIODevice::ReadOnly);
    5. engine.evaluate(file.readAll(), scriptFileName);
    6. file.close();
    7. QScriptValue global = engine.globalObject();
    8. QScriptValue GeneralCommand = global.property("myCommand");
    9. QScriptValue ATCommand1 = global.property("Command1");
    10. QScriptValue ATCommand2 = global.property("Command2");
    11. QScriptValue ATCommand3 = global.property("Command3");
    12. QScriptValue ATCommand4 = global.property("Command4");
    13. QScriptValue ATCommand5 = global.property("Command5");
    14.  
    15. CommandString[0]=GeneralCommand.call(ATCommand1).toString();
    16. CommandString[1]=GeneralCommand.call(ATCommand2).toString();
    17. CommandString[2]=GeneralCommand.call(ATCommand3).toString();
    18. CommandString[3]=GeneralCommand.call(ATCommand4).toString();
    19. CommandString[4]=GeneralCommand.call(ATCommand5).toString();
    To copy to clipboard, switch view to plain text mode 


    this is the script file"myscriptfile.js":
    Qt Code:
    1. //! [0]
    2. function myCommand() { return this.Command;}
    3. Command1 = { Command: 'test command 1'}
    4. Command2 = { Command: ' test command 2'}
    5. Command3 = { Command: ' test command 3}
    6. Command4 = { Command: ' test command 4'}
    7. Command5 = { Command: ' test command 5'}
    8. //! [0]
    To copy to clipboard, switch view to plain text mode 

    Actually it works. However I want to do it in a better way. the script file can be like this:
    Qt Code:
    1. SendCommand('test command 1');
    2. SendCommand('test command 2');
    3. SendCommand('test command 3');
    4. SendCommand('test command 4');
    To copy to clipboard, switch view to plain text mode 
    It is simple and easy to understand. But I don't know how to configure script engine in order to write script code like this? Is there someone can tell me how to do? thanks a lot.

  2. #2
    Join Date
    Apr 2010
    Location
    Minsk, Republic of Belarus
    Posts
    19
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get value from Script file?

    First way, declare array object in script environment:
    C++
    Qt Code:
    1. QString scriptFileName(":/myscriptfile.js");// Change the script file according to the requirement.
    2. QFile file(scriptFileName);
    3. file.open(QIODevice::ReadOnly);
    4. QScriptValue jsArray = engine.evaluate(file.readAll(), scriptFileName);
    5. file.close();
    6. QStringList commands = jsArray.toVariant().toStringList();
    7. // if function work with array
    8. // sendArrayToPort(commands);
    9. // else
    10. // foreach(QString command, commands)
    11. // sendToPort(command);
    To copy to clipboard, switch view to plain text mode 
    JS
    Qt Code:
    1. ["command1", "command2", "command3",...,"commandN"]
    To copy to clipboard, switch view to plain text mode 

    Second way, wrapping a native function:
    C++
    Qt Code:
    1. QScriptValue sendToPortWrapper(QScriptContext *context, QScriptEngine* engine)
    2. {
    3. if (context->argumentCount()) {
    4. sendToPort(context->argument(0).toString());
    5. }
    6. return engine->undefinedValue();
    7. }
    8.  
    9.  
    10. {
    11. ............
    12. engine->globalObject().setProperty("sendToPort", engine->newFunction(sendToPortWrapper));
    13. ............
    14. }
    To copy to clipboard, switch view to plain text mode 
    JS
    Qt Code:
    1. sendToPort('command1');
    2. sendToPort('command2');
    3. sendToPort('command3');
    4. sendToPort('command4');
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jul 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to get value from Script file?

    Thank you very much!!

Similar Threads

  1. script
    By wookoon in forum Newbie
    Replies: 1
    Last Post: 19th July 2010, 09:47
  2. Qt Script
    By c_srikanth1984 in forum Qt Programming
    Replies: 3
    Last Post: 9th June 2009, 10:35
  3. Can I include a script from script?
    By yycking in forum Qt Programming
    Replies: 1
    Last Post: 24th April 2009, 03:01
  4. Qt script
    By rajesh_clt3 in forum Qt Programming
    Replies: 2
    Last Post: 27th July 2007, 12:40
  5. need script to change qmake .pro file
    By rajeshs in forum Qt Programming
    Replies: 5
    Last Post: 7th July 2007, 17:53

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.