Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. passing QByteArray in arg with QScriptEngine
Qt 6.11 is out! See what's new in the release blog

passing QByteArray in arg with QScriptEngine

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtsript array
5 Posts 2 Posters 1.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kickoune103
    wrote on last edited by
    #1

    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.html

    or i have automatic conversion not works?

    thanks

    Gojir4G 1 Reply Last reply
    0
    • K kickoune103

      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.html

      or i have automatic conversion not works?

      thanks

      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on last edited by
      #2

      @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

      1 Reply Last reply
      1
      • K Offline
        K Offline
        kickoune103
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kickoune103
          wrote on last edited by
          #4

          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?

          Gojir4G 1 Reply Last reply
          0
          • K kickoune103

            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?

            Gojir4G Offline
            Gojir4G Offline
            Gojir4
            wrote on last edited by
            #5

            @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})

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved