First way, declare array object in script environment:
C++
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);
QString scriptFileName(":/myscriptfile.js");// Change the script file according to the requirement.
QFile file(scriptFileName);
file.open(QIODevice::ReadOnly);
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);
To copy to clipboard, switch view to plain text mode
JS
["command1", "command2", "command3",...,"commandN"]
["command1", "command2", "command3",...,"commandN"]
To copy to clipboard, switch view to plain text mode
Second way, wrapping a native function:
C++
QScriptValue sendToPortWrapper(QScriptContext *context, QScriptEngine* engine)
{
if (context->argumentCount()) {
sendToPort(context->argument(0).toString());
}
return engine->undefinedValue();
}
{
............
engine->globalObject().setProperty("sendToPort", engine->newFunction(sendToPortWrapper));
............
}
QScriptValue sendToPortWrapper(QScriptContext *context, QScriptEngine* engine)
{
if (context->argumentCount()) {
sendToPort(context->argument(0).toString());
}
return engine->undefinedValue();
}
{
............
engine->globalObject().setProperty("sendToPort", engine->newFunction(sendToPortWrapper));
............
}
To copy to clipboard, switch view to plain text mode
JS
sendToPort('command1');
sendToPort('command2');
sendToPort('command3');
sendToPort('command4');
sendToPort('command1');
sendToPort('command2');
sendToPort('command3');
sendToPort('command4');
To copy to clipboard, switch view to plain text mode
Bookmarks