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:
Code:
QScriptEngine engine;
QString scriptFileName
(":/myscriptfile.js");
// Change the script file according to the requirement. QFile file(scriptFileName
);
engine.evaluate(file.readAll(), scriptFileName);
file.close();
QScriptValue global = engine.globalObject();
QScriptValue GeneralCommand = global.property("myCommand");
QScriptValue ATCommand1 = global.property("Command1");
QScriptValue ATCommand2 = global.property("Command2");
QScriptValue ATCommand3 = global.property("Command3");
QScriptValue ATCommand4 = global.property("Command4");
QScriptValue ATCommand5 = global.property("Command5");
CommandString[0]=GeneralCommand.call(ATCommand1).toString();
CommandString[1]=GeneralCommand.call(ATCommand2).toString();
CommandString[2]=GeneralCommand.call(ATCommand3).toString();
CommandString[3]=GeneralCommand.call(ATCommand4).toString();
CommandString[4]=GeneralCommand.call(ATCommand5).toString();
this is the script file"myscriptfile.js":
Code:
//! [0]
function myCommand() { return this.Command;}
Command1 = { Command: 'test command 1'}
Command2 = { Command: ' test command 2'}
Command3 = { Command: ' test command 3}
Command4 = { Command: ' test command 4'}
Command5 = { Command: ' test command 5'}
//! [0]
Actually it works. However I want to do it in a better way. the script file can be like this:
Code:
SendCommand('test command 1');
SendCommand('test command 2');
SendCommand('test command 3');
SendCommand('test command 4');
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.
Re: How to get value from Script file?
First way, declare array object in script environment:
C++
Code:
QString scriptFileName
(":/myscriptfile.js");
// Change the script file according to the requirement. QFile file(scriptFileName
);
QScriptValue jsArray = engine.evaluate(file.readAll(), scriptFileName);
file.close();
QStringList commands
= jsArray.
toVariant().
toStringList();
// if function work with array
// sendArrayToPort(commands);
// else
// foreach(QString command, commands)
// sendToPort(command);
JS
Code:
["command1", "command2", "command3",...,"commandN"]
Second way, wrapping a native function:
C++
Code:
QScriptValue sendToPortWrapper(QScriptContext *context, QScriptEngine* engine)
{
if (context->argumentCount()) {
sendToPort(context->argument(0).toString());
}
return engine->undefinedValue();
}
{
............
engine->globalObject().setProperty("sendToPort", engine->newFunction(sendToPortWrapper));
............
}
JS
Code:
sendToPort('command1');
sendToPort('command2');
sendToPort('command3');
sendToPort('command4');
Re: How to get value from Script file?