[Interest] Accessing Qbytearray of C++ in QML

Alejandro Exojo suy at badopi.org
Fri Nov 21 11:24:16 CET 2014


El Friday 21 November 2014, Nilesh Kokane escribió:
> Hi,
> 
>  I'm accessing the Qbytearray from c++ in the qml code and assigning it to
> a property real ,but getting error while run as *qrc:///main.qml:65: Error:
> Cannot assign [undefined] to double*.

A "real" in QML is a double in C++. How do you expect a QByteArray to be 
converted to a real? There is a QByteArray::toFloat, but that's "interpret the 
byte array as text, and parse it as a float". If you are reading binary data 
that is not going to work.

The closest representation of the original QByteArray would be a string, since 
can be seen as a string of bytes, and IIRC, you can use whatever byte value in 
a JavaScript string.

$ node
> byteArray = '\x02hi!\x03\x00'
'\u0002hi!\u0003\u0000'
> byteArray.length
6
> for (var i=0; i < byteArray.length; i++) {
... console.log(byteArray.charCodeAt(i));
... }
2
104
105
33
3
0
 
>  I've registered the c++ class in the main by
> qmlRegisterType<MainWindow>("serial", 1, 0, "serialdata"); and imported the
> serial 1.0 in the qml file. Am i wrong somewhere.

You've got an error on the QML file, line 65. This line of the C++ side is not 
very useful.

-- 
Alex (a.k.a. suy) | GPG ID 0x0B8B0BC2
http://barnacity.net/ | http://disperso.net



More information about the Interest mailing list