How to return array from runJavaScript ?
-
-
@Oolong
I would not know since you did not show the output from theqDebug()
statement, which is to discover the type of the returnedconst QVariant& boxes
. In any case, did you actually try your proposed code? Doesboxes.toJsonArray()
work, and return a non-empty array? Doesobj = value.toObject()
work? Doesobj["value"]
work? Only you know, you have the code.... -
@JonB said in How to return array from runJavaScript ?:
@Oolong
I would not know since you did not show the output from theqDebug()
statement, which is to discover the type of the returnedconst QVariant& boxes
. In any case, did you actually try your proposed code? Doesboxes.toJsonArray()
work, and return a non-empty array? Doesobj = value.toObject()
work? Doesobj["value"]
work? Only you know, you have the code....Well, i'm working on upgrading Qt version of a 3rd party application from Qt5 to Qt6. So i won't know until i compile it successfully and give it a try. Here it is the original code:
void setRadioCheckedProperty( const QString& value, const QString& name , bool checked ) { QWebElementCollection boxes = page()->mainFrame()->findAllElements("input[name="+name+']'); QString currentValue=""; for(int i=0; i<boxes.count(); ++i) { currentValue = boxes.at(i).attribute("value"); d->m_checkBoxMap[currentValue]=false; emit toggledShowProperty( currentValue, false ); } update(); }
Here it is the new:
void setRadioCheckedProperty( const QString& value, const QString& name , bool checked ) { page()->runJavaScript("document.querySelectorAll('input[name=\'" + name + "\']')", [=](const QVariant& boxes) { foreach(const QJsonValue & value, boxes.toJsonArray()) { QJsonObject obj = value.toObject(); QString currentValue = obj["value"].toString(); d->m_checkBoxMap[currentValue] = false; emit toggledShowProperty(currentValue, false); } update(); }); }
-
@Oolong
The existing code usesQWebElementCollection boxes = page()->mainFrame()->findAllElements()
. I have no idea what the return type ofrunJavaScript("document.querySelectorAll()")
might be, and until you know that you won't know what code is necessary to visit what comes back in yourconst QVariant& boxes
.