Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QtWebEngine
  4. Replacement QWebElement?
Qt 6.11 is out! See what's new in the release blog

Replacement QWebElement?

Scheduled Pinned Locked Moved Solved QtWebEngine
4 Posts 2 Posters 2.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    maximus
    wrote on last edited by maximus
    #1

    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");
            }
    

    Free Indoor Cycling Software - https://maximumtrainer.com

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @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.

      157

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maximus
        wrote on last edited by
        #3

        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


        Free Indoor Cycling Software - https://maximumtrainer.com

        1 Reply Last reply
        0
        • M Offline
          M Offline
          maximus
          wrote on last edited by
          #4

          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();");
          }
          

          Free Indoor Cycling Software - https://maximumtrainer.com

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved