Typed arrays in QML? QByteArray to JS Uint8Array back and forth interop

Viewed 1132

Although JavaScript does seem to provide a range of type arrays for efficiently storing various width integer and real numbers, those seem to be absent in the QML implementation:

​var array = new Uint8Array // error: Expected token `}

I am in need of QByteArray interop between C++ and JS, and it is not as trivial as making QByteArray a metatype so it can be used in QML as a parameter - I need persistent hard copies of the data living in JS, although I will not be modifying the data from JS and will only be used in the exposed C++ API.

I currently have a solution based on converting back and from using QString and Latin1 conversion, and even though it does seem to pass simple unit tests, it feels flimsy and unsafe, so I wonder if there is a more elegant and cleaner solution? Also, it is somewhat inefficient, as it would use two bytes for every byte stored in it.

So it turned out that:

  • as Mitch pointed out, there is a "ghost" character which messes up the code
  • Creator's autocomplete doesn't detect Uint8Array, which reinforced my suspicion that it isn't there

This code snippet confirms it is there and working:

  var array = new Uint8Array(1)
  array[0] = 257
  console.log(array[0]) // outputs 1, the expected overflow value

But the significant part of the question remains unanswered - how to (efficiently) interop between QByteArray and Uint8Array. The key here is efficiency as I have plenty of those interops, and I'd hate to use something as clumsy as filling in Uint8Array by calling JS functions from C++ one element at a time.

1 Answers
Related