Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Passing QByteArray to a callable QJSValue
QtWS25 Last Chance

Passing QByteArray to a callable QJSValue

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
qjsvalueqbytearray
3 Posts 3 Posters 1.3k Views
  • 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.
  • J Offline
    J Offline
    jernkuan
    wrote on last edited by
    #1

    What is the correct way to pass a QByteArray as a parameter to a callable QJSValue?

    Assuming data is binary format.

    QString title = "Intro";
    QByteArray byteArray;
    // read from file into byteArray
    
    QJSValueList args;
    args << title;
    args << byteArray;
    callback.call(args);
    

    The javascript function will be subsequently be passing this to a C++ function for processing.

    Should i be doing this?

    QString title = "Intro";
    QByteArray byteArray;
    // read from file into byteArray
    
    QJSValueList args;
    args << title;
    args << QString::fromLatin1(byteArray.data(), payload.size());
    callback.call(args);
    
    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rick Smith 2099
      wrote on last edited by
      #2

      workaround:

      1. convert QByteArray to QString(base64 encoded)
      2. decode that string on qml/js side
      QByteArray ba = ...;
      QString encoed(ba.toBase64());
      QJSValueList params { QJSValue(encoed)};
      callback.call(params);
      

      waiting for better anser :)

      1 Reply Last reply
      1
      • C Offline
        C Offline
        Colton
        wrote on last edited by
        #3

        In the event that anyone else comes across this looking for a solution (like I was), Qt 5 and above can convert QByteArray to a Javascript ArrayBuffer.
        To do this, use toScriptValue:

        QJSEngine jsEngine = qjsEngine(this);
        QJSValueList args;
        args << title;
        args << jsEngine->toScriptValue(byteArray);
        callback.call(args);
        
        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