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.