Reputation: 49309
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:
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.
Upvotes: 3
Views: 1441
Reputation: 24416
Heh, this is weird. Typed arrays are indeed supported in Qt 5.5. I had to ask a friend why that code didn't work. He pointed out that you have a zero-width space before the var
keyword. I can't see this in Qt Creator, even with "Visualize whitespace" turned on, but vi
shows it:
<200b>var array = new Uint8Array
He said it is often generated; perhaps by buggy editors.
The QML parser should pick this up though; see QTBUG-51881.
Upvotes: 1