Replacement QWebElement?



  • I am migrating to QWebEngine from QWebKit.
    It's going well so far, I am just stuck with the use of QWebElement.

    Before I was checking the value of some input field in my webPage.
    Is this possible with runJavascript? Can runJavascript return the value of an Input field on a webpage?

    Cheers,

    Old code:

            QWebElement inputNameElement = ui->webView_createWorkout->page()->mainFrame()->documentElement().findFirst("input[id=\"name-workout\"]");
            QWebElement inputCreatorElement = ui->webView_createWorkout->page()->mainFrame()->documentElement().findFirst("input[id=\"creator-workout\"]");
            QString valueName =  inputNameElement.evaluateJavaScript("this.value").toString();
            QString valueCreator =  inputCreatorElement.evaluateJavaScript("this.value").toString();
    
            if (valueName.size() == 0 || valueCreator.size() == 0) {
                enabled = false;
            }
    
            if (enabled) {
                buttonSaveElement.removeAttribute("disabled");
            }
            else {
                buttonSaveElement.setAttribute("disabled", "disabled");
            }
    

  • Moderators

    @maximus Yes it is quite possible using runJavaScript. Recently I too had a similar requirement. Try something similar as following:

    page()->runJavaScript("document.getElementsByClassName(\"auth-box\")[0].getAttribute(\"data-token\");"
                                          ,[=](const QVariant &result){
                        QString code = result.toString();
                    });
    

    The above code searches element by class name and returns result via a callback. You can modify it easily to search it by id or tag.



  • Thanks for the code, I will try that.
    Or maybe just coding it in JS and call the JS method? I will see what is easier :)
    Cheers,
    Max



  • Here is my solution if anyone is interested. I had problem adding the js directly in the html so I used injection

    QWebEngineProfile *profile = new QWebEngineProfile("MyWebChannelProfile", this);
    
     //    QWebEngineProfile *profile = qApp->property("WebEngineProfile").value<QWebEngineProfile*>();
    
     QFile webChannelJsFile(":/qtwebchannel/qwebchannel.js");
     if(  !webChannelJsFile.open(QIODevice::ReadOnly) ) {
         qDebug() << QString("Couldn't open qwebchannel.js file: %1").arg(webChannelJsFile.errorString());
     }
     else {
         qDebug() << "OK webEngineProfile";
         QByteArray webChannelJs = webChannelJsFile.readAll();
         webChannelJs.append(
                     "\n"
                     "var workoutCreator"
                     "\n"
                     "new QWebChannel(qt.webChannelTransport, function(channel) {"
                     "     workoutCreator = channel.objects.workoutCreator;"
                     "});"
                     "\n"
                     "function enableSaveButton() {"
                     "var nameValue = $('#name-workout').val();"
                     "var planValue =   $('#plan-workout').val();"
                     "var creatorValue = $('#creator-workout').val();"
                     "if (nameValue.length > 0 && creatorValue.length > 0 && planValue.length > 0) {$('#btn-save-workout').prop('disabled', false);}"
                     "else {$('#btn-save-workout').prop('disabled', true);}"
                     "}"
                     );
    
         QWebEngineScript script;
         script.setSourceCode(webChannelJs);
         script.setName("qwebchannel.js");
         script.setWorldId(QWebEngineScript::MainWorld);
         script.setInjectionPoint(QWebEngineScript::DocumentCreation);
         script.setRunsOnSubFrames(false);
         profile->scripts()->insert(script);
     }
    
     QWebEnginePage *myPage = new QWebEnginePage(profile, ui->webView_createWorkout);
     ui->webView_createWorkout->setPage(myPage);
    
     QWebChannel *channel = new QWebChannel(myPage);
     ui->webView_createWorkout->page()->setWebChannel(channel);
     channel->registerObject("workoutCreator", this);
    }
    
    void WorkoutCreator::checkToEnableButtonSave() {
        if (intervalModel->rowCount() <= 0) {
            return;
        }
        //call JS to activate button!
        ui->webView_createWorkout->page()->runJavaScript("enableSaveButton();");
    }
    

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.