Sending a list of javascript objects from QML to c++









up vote
0
down vote

favorite












I have an invokable c++ function that I call from a QML file. I would like this function to accept a list of javascript objects (or maps) as an argument. The Qt documentation indicates that javascript lists are translated to QVariantList, and javascript objects to QVariantMap. (see here, last paragraph of the section).




Similarly, if a C++ type uses a QVariantList or QVariantMap type for a property type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++.




Unfortunately, when the argument is typed as a QVariantList, QList<QVariant> or even QVariant(then translated to a QList<QVariant>) the length returned is 0. I also tried having it typed as a QList<QObject*> and the length of the list can be read, but I can't access the data inside as a QObject* cannot be cast to a QVariant.



QML code excerpt:



for(currentLabel = 0; currentLabel<acquisitionsLabels.length; currentLabel++)
label = acquisitionsLabels[currentLabel];
link = db.getLinkFromLabel(label)[0];
structureName="";
if(link)
criteria = db.getCriteriaFromId(link.fkCriteria);
structure = db.getStructureFromId(criteria.fkStructure);
structureName = structure.name;

labels.push(
"label":label,
"structureName":structureName,
"moreToCome":currentLabel<acquisitionsLabels.length-1
);

console.log(labels.length);
exportReportFile.printAcquisition(acquisition, acquisitionRenderer.renderAcquisition(acquisition), labels);


exportReportFile.cpp excerpt:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QVariant& a_labels)
QList<QVariant> l_labels = a_labels.value<QList<QVariant>>();
std::cout << typeid(l_labels).name() << std::endl;
std::cout << l_labels.count() << std::endl;
if(l_labels.length()>0)
for(int i=0;i<l_labels.count();i++)
auto item = l_labels.at(i);
std::cout << typeid(item).name() << std::endl;





This version is with the argument being a QVariant reference, but I also tried with all previously cited types.



The expected output for a list that is 3 items long would be:



qml: 3
class QList<class QVariant>
3
class QVariant
class QVariant
class QVariant


and it actually is



qml: 3
class QList<class QVariant>
0









share|improve this question





















  • try to use const QVariantList & labels instead const QVariant& a_labels
    – L.V.A
    14 hours ago










  • Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
    – folibis
    14 hours ago










  • Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
    – SThor
    14 hours ago











  • Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
    – SThor
    14 hours ago














up vote
0
down vote

favorite












I have an invokable c++ function that I call from a QML file. I would like this function to accept a list of javascript objects (or maps) as an argument. The Qt documentation indicates that javascript lists are translated to QVariantList, and javascript objects to QVariantMap. (see here, last paragraph of the section).




Similarly, if a C++ type uses a QVariantList or QVariantMap type for a property type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++.




Unfortunately, when the argument is typed as a QVariantList, QList<QVariant> or even QVariant(then translated to a QList<QVariant>) the length returned is 0. I also tried having it typed as a QList<QObject*> and the length of the list can be read, but I can't access the data inside as a QObject* cannot be cast to a QVariant.



QML code excerpt:



for(currentLabel = 0; currentLabel<acquisitionsLabels.length; currentLabel++)
label = acquisitionsLabels[currentLabel];
link = db.getLinkFromLabel(label)[0];
structureName="";
if(link)
criteria = db.getCriteriaFromId(link.fkCriteria);
structure = db.getStructureFromId(criteria.fkStructure);
structureName = structure.name;

labels.push(
"label":label,
"structureName":structureName,
"moreToCome":currentLabel<acquisitionsLabels.length-1
);

console.log(labels.length);
exportReportFile.printAcquisition(acquisition, acquisitionRenderer.renderAcquisition(acquisition), labels);


exportReportFile.cpp excerpt:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QVariant& a_labels)
QList<QVariant> l_labels = a_labels.value<QList<QVariant>>();
std::cout << typeid(l_labels).name() << std::endl;
std::cout << l_labels.count() << std::endl;
if(l_labels.length()>0)
for(int i=0;i<l_labels.count();i++)
auto item = l_labels.at(i);
std::cout << typeid(item).name() << std::endl;





This version is with the argument being a QVariant reference, but I also tried with all previously cited types.



The expected output for a list that is 3 items long would be:



qml: 3
class QList<class QVariant>
3
class QVariant
class QVariant
class QVariant


and it actually is



qml: 3
class QList<class QVariant>
0









share|improve this question





















  • try to use const QVariantList & labels instead const QVariant& a_labels
    – L.V.A
    14 hours ago










  • Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
    – folibis
    14 hours ago










  • Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
    – SThor
    14 hours ago











  • Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
    – SThor
    14 hours ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have an invokable c++ function that I call from a QML file. I would like this function to accept a list of javascript objects (or maps) as an argument. The Qt documentation indicates that javascript lists are translated to QVariantList, and javascript objects to QVariantMap. (see here, last paragraph of the section).




Similarly, if a C++ type uses a QVariantList or QVariantMap type for a property type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++.




Unfortunately, when the argument is typed as a QVariantList, QList<QVariant> or even QVariant(then translated to a QList<QVariant>) the length returned is 0. I also tried having it typed as a QList<QObject*> and the length of the list can be read, but I can't access the data inside as a QObject* cannot be cast to a QVariant.



QML code excerpt:



for(currentLabel = 0; currentLabel<acquisitionsLabels.length; currentLabel++)
label = acquisitionsLabels[currentLabel];
link = db.getLinkFromLabel(label)[0];
structureName="";
if(link)
criteria = db.getCriteriaFromId(link.fkCriteria);
structure = db.getStructureFromId(criteria.fkStructure);
structureName = structure.name;

labels.push(
"label":label,
"structureName":structureName,
"moreToCome":currentLabel<acquisitionsLabels.length-1
);

console.log(labels.length);
exportReportFile.printAcquisition(acquisition, acquisitionRenderer.renderAcquisition(acquisition), labels);


exportReportFile.cpp excerpt:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QVariant& a_labels)
QList<QVariant> l_labels = a_labels.value<QList<QVariant>>();
std::cout << typeid(l_labels).name() << std::endl;
std::cout << l_labels.count() << std::endl;
if(l_labels.length()>0)
for(int i=0;i<l_labels.count();i++)
auto item = l_labels.at(i);
std::cout << typeid(item).name() << std::endl;





This version is with the argument being a QVariant reference, but I also tried with all previously cited types.



The expected output for a list that is 3 items long would be:



qml: 3
class QList<class QVariant>
3
class QVariant
class QVariant
class QVariant


and it actually is



qml: 3
class QList<class QVariant>
0









share|improve this question













I have an invokable c++ function that I call from a QML file. I would like this function to accept a list of javascript objects (or maps) as an argument. The Qt documentation indicates that javascript lists are translated to QVariantList, and javascript objects to QVariantMap. (see here, last paragraph of the section).




Similarly, if a C++ type uses a QVariantList or QVariantMap type for a property type or method parameter, the value can be created as a JavaScript array or object in QML, and is automatically converted to a QVariantList or QVariantMap when it is passed to C++.




Unfortunately, when the argument is typed as a QVariantList, QList<QVariant> or even QVariant(then translated to a QList<QVariant>) the length returned is 0. I also tried having it typed as a QList<QObject*> and the length of the list can be read, but I can't access the data inside as a QObject* cannot be cast to a QVariant.



QML code excerpt:



for(currentLabel = 0; currentLabel<acquisitionsLabels.length; currentLabel++)
label = acquisitionsLabels[currentLabel];
link = db.getLinkFromLabel(label)[0];
structureName="";
if(link)
criteria = db.getCriteriaFromId(link.fkCriteria);
structure = db.getStructureFromId(criteria.fkStructure);
structureName = structure.name;

labels.push(
"label":label,
"structureName":structureName,
"moreToCome":currentLabel<acquisitionsLabels.length-1
);

console.log(labels.length);
exportReportFile.printAcquisition(acquisition, acquisitionRenderer.renderAcquisition(acquisition), labels);


exportReportFile.cpp excerpt:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QVariant& a_labels)
QList<QVariant> l_labels = a_labels.value<QList<QVariant>>();
std::cout << typeid(l_labels).name() << std::endl;
std::cout << l_labels.count() << std::endl;
if(l_labels.length()>0)
for(int i=0;i<l_labels.count();i++)
auto item = l_labels.at(i);
std::cout << typeid(item).name() << std::endl;





This version is with the argument being a QVariant reference, but I also tried with all previously cited types.



The expected output for a list that is 3 items long would be:



qml: 3
class QList<class QVariant>
3
class QVariant
class QVariant
class QVariant


and it actually is



qml: 3
class QList<class QVariant>
0






javascript c++ qt qml






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 15 hours ago









SThor

134




134











  • try to use const QVariantList & labels instead const QVariant& a_labels
    – L.V.A
    14 hours ago










  • Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
    – folibis
    14 hours ago










  • Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
    – SThor
    14 hours ago











  • Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
    – SThor
    14 hours ago
















  • try to use const QVariantList & labels instead const QVariant& a_labels
    – L.V.A
    14 hours ago










  • Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
    – folibis
    14 hours ago










  • Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
    – SThor
    14 hours ago











  • Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
    – SThor
    14 hours ago















try to use const QVariantList & labels instead const QVariant& a_labels
– L.V.A
14 hours ago




try to use const QVariantList & labels instead const QVariant& a_labels
– L.V.A
14 hours ago












Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
– folibis
14 hours ago




Please post only relevant code that could be run. Please remove all unnecessary code, like access to DB etc. Read how to create Minimal, Complete, and Verifiable example.
– folibis
14 hours ago












Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
– SThor
14 hours ago





Thanks :) I tried this a few times, with the same result. I'm thinking maybe it has something to do with the type of the label that I put inside the JS object ? @folibis ok, I'll try to clean it up.
– SThor
14 hours ago













Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
– SThor
14 hours ago




Problem found, thanks : in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. Sorry for the bother.
– SThor
14 hours ago












1 Answer
1






active

oldest

votes

















up vote
0
down vote













Create a method that takes a QJSValue as parameter. This way the value is not converted to QVariant but the actual JS data is returned. You can then continue doing stuff with that as you please, including the conversion to QVariant, if needed:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QJSValue& a_labels) 
// do stuff with it:
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i)
auto arrayElement = jsArray.property(i);
// now you have the element etc.







share|improve this answer




















  • In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
    – SThor
    10 hours ago










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204400%2fsending-a-list-of-javascript-objects-from-qml-to-c%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













Create a method that takes a QJSValue as parameter. This way the value is not converted to QVariant but the actual JS data is returned. You can then continue doing stuff with that as you please, including the conversion to QVariant, if needed:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QJSValue& a_labels) 
// do stuff with it:
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i)
auto arrayElement = jsArray.property(i);
// now you have the element etc.







share|improve this answer




















  • In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
    – SThor
    10 hours ago














up vote
0
down vote













Create a method that takes a QJSValue as parameter. This way the value is not converted to QVariant but the actual JS data is returned. You can then continue doing stuff with that as you please, including the conversion to QVariant, if needed:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QJSValue& a_labels) 
// do stuff with it:
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i)
auto arrayElement = jsArray.property(i);
// now you have the element etc.







share|improve this answer




















  • In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
    – SThor
    10 hours ago












up vote
0
down vote










up vote
0
down vote









Create a method that takes a QJSValue as parameter. This way the value is not converted to QVariant but the actual JS data is returned. You can then continue doing stuff with that as you please, including the conversion to QVariant, if needed:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QJSValue& a_labels) 
// do stuff with it:
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i)
auto arrayElement = jsArray.property(i);
// now you have the element etc.







share|improve this answer












Create a method that takes a QJSValue as parameter. This way the value is not converted to QVariant but the actual JS data is returned. You can then continue doing stuff with that as you please, including the conversion to QVariant, if needed:



void ExportReportFile::printAcquisition(QObject *a_acquisition, const QJSValue& a_labels) 
// do stuff with it:
const int length = jsArray.property("length").toInt();
for (int i = 0; i < length; ++i)
auto arrayElement = jsArray.property(i);
// now you have the element etc.








share|improve this answer












share|improve this answer



share|improve this answer










answered 13 hours ago









Felix

4,19311636




4,19311636











  • In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
    – SThor
    10 hours ago
















  • In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
    – SThor
    10 hours ago















In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
– SThor
10 hours ago




In the end this was not the problem. QVariantList, QList<QVariant>, QVariant(then translated to a QList<QVariant>) all work. The problem was that in the QML call of the C++ method, I had 3 arguments, while the C++ code only asked for 2. I am not sure why an error didn't show up.
– SThor
10 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204400%2fsending-a-list-of-javascript-objects-from-qml-to-c%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ