0

In qml,

templist:
[{"product_code":"111111111","product_name":"AAAA"},
{"product_code":"222222222","product_name":"BBBB"},
{"product_code":"33333333","product_name":"CCCC"}]

with the help of below code in the qml side, the above templist sent to c++ side as a Qstring ,

function listToString() {
    var data = []
    for (var i = 0; i < templist.count; ++i) {
        data.push(templist.get(i))
    }
    var keysList = JSON.stringify(data)
    console.log(keysList)
    **Option A:**  backend.request_add(keysList)
    **Option B:**  backend.request_add(data)
}

in the C++ side,

Option A: keysList as multidata
Option B: data as multidata

I got the above input converted into a

QByteArray br = multidata.toUtf8();

Option A

br = 
[{\"product_code\":\"111111111\",\"product_name\":\"AAAA\"},
{\"product_code\":\"222222222\",\"product_name\":\"BBBB\"},
{\"product_code\":\"33333333\",\"product_name\":\"CCCC\"}]

Option B

br = "QObject(0x560034863a60),QObject(0x5600348628b0),QObject(0x7f76000074d0)"

Question: In Option A, I have converted the key pair to json format before sending it to c++ side as a qstring. is there a way to get the key-pair from Option B directly from this output

br = "QObject(0x560034863a60),QObject(0x5600348628b0),QObject(0x7f76000074d0)"

if I convert in the qml side itself I get the desired answer listed in Option A

br = [{"product_code":"111111111","product_name":"AAAA"},
{"product_code":"222222222","product_name":"BBBB"},
{"product_code":"33333333","product_name":"CCCC"}]

I'm trying to achieve the same desired output using Option B. Please point me in the right direction

iam_peter
  • 3,205
  • 1
  • 19
  • 31
  • Why didn't you use [QVariant](https://doc.qt.io/qt-6/qvariant.html) to send the `JavaScript value` to `C++`? However, if you only have access to a json string, you can use the [QJsonDocument::fromJson](https://doc.qt.io/qt-6/qjsondocument.html#fromJson) function to parse it. – SMR Dec 02 '22 at 04:51
  • Does this answer your question? [QML: passing JS object to C++ member function](https://stackoverflow.com/questions/47287213/qml-passing-js-object-to-c-member-function) – SMR Dec 02 '22 at 08:27

1 Answers1

0

Option B will never work. You convert an object instance into a stringified version. This is basically the type (QObject) and its adresse (0x560034863a60).

You have to stringify the JSON data with JSON.stringify(data) to be able to transfer the data as JSON string to the C++ side.

What would be the advantage of Option B anyway?

Jürgen Lutz
  • 329
  • 1
  • 1
  • 10
  • can the data read from the QObject(0x560034863a60) from this address in c++? – Rajesh Kannan Dec 01 '22 at 15:09
  • No. What you can do is forget about the JSON communication and register a C++ class to store your data directly. – Jürgen Lutz Dec 01 '22 at 15:19
  • It is inefficient and prohibited to pass **JavaScript values** to `C++` through `string`. [QVariant](https://doc.qt.io/qt-6/qvariant.html) and `QVariantMap` are offered by Qt, making it much easier to obtain **JavaScript values** as [QVariant](https://doc.qt.io/qt-6/qvariant.html) in `C++`. – SMR Dec 02 '22 at 04:59