Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to return array from runJavaScript ?



  • I want access each elements after i run the javascript below.

    page()->runJavaScript("document.querySelectorAll('input[name=\'" + name + "\']')", [](const QVariant& boxes)
    {
        // how do i access boxes[0], boxes[1] .... boxes[n] ? 
    });
    

    So how can i do that ?



  • @Oolong
    qDebug() << boxes.type() << boxes.typeName();



  • @JonB Thanks for the answer.

    Can i also convert boxes to a QJsonObject and use it like this?

    foreach(const QJsonValue & value, boxes.toJsonArray())
    {
        QJsonObject obj = value.toObject();
        QString currentValue = obj["value"].toString();
    }


  • @Oolong
    I would not know since you did not show the output from the qDebug() statement, which is to discover the type of the returned const QVariant& boxes. In any case, did you actually try your proposed code? Does boxes.toJsonArray() work, and return a non-empty array? Does obj = value.toObject() work? Does obj["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 the qDebug() statement, which is to discover the type of the returned const QVariant& boxes. In any case, did you actually try your proposed code? Does boxes.toJsonArray() work, and return a non-empty array? Does obj = value.toObject() work? Does obj["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 uses QWebElementCollection boxes = page()->mainFrame()->findAllElements(). I have no idea what the return type of runJavaScript("document.querySelectorAll()") might be, and until you know that you won't know what code is necessary to visit what comes back in your const QVariant& boxes.


Log in to reply