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
javascript c++ qt qml
add a comment |
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
javascript c++ qt qml
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 thelabelthat 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
add a comment |
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
javascript c++ qt qml
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
javascript c++ qt qml
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 thelabelthat 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
add a comment |
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 thelabelthat 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
add a comment |
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.
In the end this was not the problem.QVariantList,QList<QVariant>,QVariant(then translated to aQList<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
add a comment |
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.
In the end this was not the problem.QVariantList,QList<QVariant>,QVariant(then translated to aQList<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
add a comment |
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.
In the end this was not the problem.QVariantList,QList<QVariant>,QVariant(then translated to aQList<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
add a comment |
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.
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.
answered 13 hours ago
Felix
4,19311636
4,19311636
In the end this was not the problem.QVariantList,QList<QVariant>,QVariant(then translated to aQList<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
add a comment |
In the end this was not the problem.QVariantList,QList<QVariant>,QVariant(then translated to aQList<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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
labelthat 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