Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does QByteArray get converted to in QML?

If I have a

signal:
    void foo(QByteArray data);

And in QML I handle it:

 onFoo: {
     console.log(data.toString());
 }

What is the type of data in QML? What methods does it have? It doesn't seem to be a javascript string - it doesn't even have a .length, and no .charCodeAt(). But it also doesn't seem to be a QByteArray - no .at(). data[0] is undefined! It does have .toString().

How do I access its contents? E.g. if it is a four-byte uint32_t, how do I get that number into a javascript variable?

like image 323
Timmmm Avatar asked Jun 05 '15 13:06

Timmmm


People also ask

What is QByteArray in Qt?

QByteArray is the Qt class to store a array of bytes. It is analogous to a char *. Unlike a QString, a QByteArray has no encoding information and makes no attempt to decode bytes into characters.

How do you initialize a QByteArray?

One way to initialize a QByteArray is simply to pass a const char * to its constructor. For example, the following code creates a byte array of size 5 containing the data "Hello": QByteArray ba("Hello");


2 Answers

As stated in here :

The QML engine provides automatic type conversion between QByteArray values and JavaScript ArrayBuffer objects.

This feature is available since Qt 5.8.

like image 162
Nejat Avatar answered Oct 20 '22 14:10

Nejat


According to http://doc.qt.io/qt-5/qtqml-cppintegration-data.html and the current source http://code.woboq.org/qt5/qtdeclarative/src/qml/compiler/qqmltypecompiler.cpp.html#567, there's no conversion between QByteArray towards QML. You'll probably get an opaque, unaccesible Object in JS.

EDIT: The above was true up to Qt 5.8. 5.8 added a conversion between QByteArray and JS Array, see the other answer.

Needless to say: at the time of this writing, the only version you want to be using is >= 5.12.

like image 3
peppe Avatar answered Oct 20 '22 15:10

peppe