passing QByteArray in arg with QScriptEngine
-
hello,
i will try to use QScriptEngine to write function to Read Array and write array to change Trame for example with qtscript user.
i have read lot of documentation.i will try this:
for the moment, i have QByteArray in my software, to check trameProtocol by script.QByteArray l_ByteArray ="01"; QVariant l_varByteArray = l_ByteArray; QScriptValue l_var = m_engine.newVariant(l_varByteArray); QScriptValue m_fct_checksum = m_engine.evaluate("( \n function cal_crc(pTrame) \n { return pTrame[0]; \n } \n)"); qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toInteger(); /* works can read if i return element of array, but not can t modify by script , not have properti length in script not work
if call with newArray(), not have to access to property length */
i don't understand i will write custom class like this
https://doc.qt.io/archives/qt-4.8/qt-script-customclass-example.htmlor i have automatic conversion not works?
thanks
-
@kickoune103 Hi,
Here is the function I'm using to convert JsArray to QByteArray and opposite.
static QByteArray qByteArrayFromJSArray(const QScriptValue &jsArray) { QByteArray ba; if(jsArray.isNull() || !jsArray.isArray()) return ba; QScriptValueIterator it(jsArray); bool indexOk; while (it.hasNext()) { it.next(); it.name().toUInt(&indexOk); //take only property name composed by a number. Otherwise this is not a data value if(indexOk) ba.append(static_cast<quint8>(it.value().toUInt16())); } return ba; }
static QScriptValue jsArrayFromQByteArray(const QByteArray &ba, QScriptEngine *jsEngine) { QScriptValue array = jsEngine->newArray(ba.size()); for(int i = 0; i < ba.size(); i++) array.setProperty(i, ba.at(i) & 0xFF); return array; }
Hope this helps
-
ok i saw,
but i have use this with this script:
"( \n function cal_crc(pTrame) \n { return pTrame.length; \n } \n)"QScriptValue l_var = m_engine.newArray(2); l_var.setProperty( 0,QScriptValue('0') ); l_var.setProperty( 1,QScriptValue('1') ); qDebug() << l_var.toVariant(); qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toInteger(); //console QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49))) 0
like writing previously, don't modify pTrame in script
("(function cal_crc(pTrame){pTrame[0]=5;return pTrame[0];})"); //same result QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49))) 0 //not 5
if i return pTrame in script:
"(function cal_crc(pTrame){pTrame[0]=5;return pTrame;})" qDebug() << m_fct_checksum.call(QScriptValue(),l_var).toVariant(); QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49))) QVariant(int, 48)
or
"function cal_crc(pTrame){return pTrame})" QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49))) QVariant(int, 48) //return only the first,, it's strange because en i work in arg but not in return and can't modify i repeat..
any idea?
thanks
-
if i register pTrame is ok and not use in arg
//with this script: "(function(){pTrame[0]=5;return pTrame})"QScriptValue l_var = m_engine.newArray(2);
l_var.setProperty( 0,QScriptValue('0') );
l_var.setProperty( 1,QScriptValue('1') );
m_engine.globalObject().setProperty("pTrame", l_var);//console
QVariant(QVariantList, (QVariant(int, 48), QVariant(int, 49)))
QVariant(QVariantList, (QVariant(int, 5), QVariant(int, 49)))any idea to do better?
-
@kickoune103 What do you mean by "if I register pTrame" ?
This is not clear for me when your C++ code is called, is it when
crc_calc
is called ?It may be ambiguous to use the same name for your global property and the function's argument.
->m_engine.globalObject().setProperty("pTrame", l_var);
->function cal_crc(pTrame){return pTrame})
I suggest that you change the name of the arg:
->function cal_crc(trame){trame[0]=5;return trame})