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 941 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 12 Jan 2022, 06:48 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 ?

    J 1 Reply Last reply 12 Jan 2022, 07:20
    0
    • O Oolong
      12 Jan 2022, 06:48

      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 ?

      J Online
      J Online
      JonB
      wrote on 12 Jan 2022, 07:20 last edited by
      #2

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

      O 1 Reply Last reply 12 Jan 2022, 07:35
      0
      • J JonB
        12 Jan 2022, 07:20

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

        O Offline
        O Offline
        Oolong
        wrote on 12 Jan 2022, 07:35 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();
        }
        
        J 1 Reply Last reply 12 Jan 2022, 07:40
        0
        • O Oolong
          12 Jan 2022, 07:35

          @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();
          }
          
          J Online
          J Online
          JonB
          wrote on 12 Jan 2022, 07:40 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 12 Jan 2022, 07:53
          0
          • J JonB
            12 Jan 2022, 07:40

            @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 12 Jan 2022, 07:53 last edited by Oolong 1 Dec 2022, 07:55
            #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();
                });
            }
            
            J 1 Reply Last reply 12 Jan 2022, 08:02
            0
            • O Oolong
              12 Jan 2022, 07:53

              @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();
                  });
              }
              
              J Online
              J Online
              JonB
              wrote on 12 Jan 2022, 08:02 last edited by JonB 1 Dec 2022, 08:02
              #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

              1/6

              12 Jan 2022, 06:48

              • Login

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