Alvaro Lorente
Alvaro Lorente

Reputation: 3084

QByteArray to Javascript Buffer or serializable type in QML

I have registered a class that returns the result of reading a file this is of the type QByteArray, that seems not to be a natively supported file in QML, so I get some gibberish, even if its the correct data I cannot convert it to a blob.

So i am trying to get at the end a Javascript type of File or Blob that itself is the end goal. So this 2 javascript types do not exist in QML.

My question is how can I create a bufferish type that go through a serialized Json object to my embedded web.

PS. even being a bad idea i have even try QString but i have lost data in the conversion of QByteArray to QString

Upvotes: 2

Views: 2045

Answers (2)

dtech
dtech

Reputation: 49309

What I did for my needs was to wrap a combination of QByteArray, QBuffer and QDataStream in a QObject together with functions to read and write primitive types and save to / load from disk and some other auxiliary functionality, to create what turned out to be a very useful BinaryData type which as a QObject derived , can be freely passed around, without any potentially costly conversions taking place.

This allows to use it in an identical way in both C++ and QML, if needed you can easily make a toArrayBuffer() method for it.

Upvotes: 1

Velkan
Velkan

Reputation: 7592

It's ArrayBuffer since Qt 5.8.

2016-03-05 3b7e2a69f7eb8597c807de39b4de39721e9e2bd2

Convert QByteArray in C++ type to ArrayBuffer in QML and vice-versa

Nobuaki Sukegawa, Reviewed-by: Simon Hausmann.

SO mentions

Qt tracker somewhat related mentions

Upvotes: 3

Related Questions