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. How to return array from runJavaScript ?
Forum Updated to NodeBB v4.3 + New Features

How to return array from runJavaScript ?

Scheduled Pinned Locked Moved Unsolved QtWebEngine
6 Posts 2 Posters 942 Views
  • 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.
  • O Offline
    O Offline
    Oolong
    wrote on last edited by
    #1

    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 ?

    JonBJ 1 Reply Last reply
    0
    • O Oolong

      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 ?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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

      O 1 Reply Last reply
      0
      • JonBJ JonB

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

        O Offline
        O Offline
        Oolong
        wrote on last edited by
        #3

        @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();
        }
        
        JonBJ 1 Reply Last reply
        0
        • O Oolong

          @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();
          }
          
          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

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

          O 1 Reply Last reply
          0
          • JonBJ JonB

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

            O Offline
            O Offline
            Oolong
            wrote on last edited by Oolong
            #5

            @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();
                });
            }
            
            JonBJ 1 Reply Last reply
            0
            • O Oolong

              @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();
                  });
              }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

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

              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