最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does QByteArray get converted to in QML? - Stack Overflow

programmeradmin6浏览0评论

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?

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?

Share Improve this question asked Jun 5, 2015 at 13:38 TimmmmTimmmm 96.7k80 gold badges405 silver badges573 bronze badges 1
  • 1 There seems to be some development in getting QByteArray to ArrayBuffer conversion. Since it was merged just a few days ago we probably have to wait until Qt 5.8. codereview.qt-project/#/c/144761 – pwuertz Commented Apr 17, 2016 at 22:33
Add a ment  | 

2 Answers 2

Reset to default 9

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.

According to http://doc.qt.io/qt-5/qtqml-cppintegration-data.html and the current source http://code.woboq/qt5/qtdeclarative/src/qml/piler/qqmltypepiler.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.

发布评论

评论列表(0)

  1. 暂无评论