Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWebEngineView. How to pass variable to Js function?
Forum Updated to NodeBB v4.3 + New Features

QWebEngineView. How to pass variable to Js function?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.4k 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.
  • U Offline
    U Offline
    Ucn_
    wrote on last edited by Ucn_
    #1

    Hello. I would to know how I can pass a QString variable to js function. Example:

    QString value;
    webview = new QWebEngineView;
    webview->page()->runJavaScript("getInfo(value);");
    

    I receive this error:

    js: Uncaught ReferenceError: value is not defined
    [17240:14380:1213/204829.811:INFO:CONSOLE(1)] "Uncaught ReferenceError: value is not defined", source: qrc:/new/web/Posts.html (1)
    

    It only accepts if value is quoted, "value".
    Thanks

    JonBJ 1 Reply Last reply
    0
    • U Ucn_

      @JonB Thanks, can that method pass two parameters?

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

      @Ucn_

      webview->page()->runJavaScript("getInfo('" + value1 + "', '" + value2  + "');");
      
      U 1 Reply Last reply
      1
      • U Ucn_

        Hello. I would to know how I can pass a QString variable to js function. Example:

        QString value;
        webview = new QWebEngineView;
        webview->page()->runJavaScript("getInfo(value);");
        

        I receive this error:

        js: Uncaught ReferenceError: value is not defined
        [17240:14380:1213/204829.811:INFO:CONSOLE(1)] "Uncaught ReferenceError: value is not defined", source: qrc:/new/web/Posts.html (1)
        

        It only accepts if value is quoted, "value".
        Thanks

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

        @Ucn_
        To pass the value of value into your JavaScript, as shown, you'll want something like

        webview->page()->runJavaScript("getInfo('" + value + "');");
        

        Note how it is being quoted inside ' and '.

        There will be some alternative QString way of making the string I do with + above, if you prefer.

        U 1 Reply Last reply
        1
        • JonBJ JonB

          @Ucn_
          To pass the value of value into your JavaScript, as shown, you'll want something like

          webview->page()->runJavaScript("getInfo('" + value + "');");
          

          Note how it is being quoted inside ' and '.

          There will be some alternative QString way of making the string I do with + above, if you prefer.

          U Offline
          U Offline
          Ucn_
          wrote on last edited by
          #3

          @JonB Thanks, can that method pass two parameters?

          JonBJ 1 Reply Last reply
          0
          • U Ucn_

            @JonB Thanks, can that method pass two parameters?

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

            @Ucn_

            webview->page()->runJavaScript("getInfo('" + value1 + "', '" + value2  + "');");
            
            U 1 Reply Last reply
            1
            • JonBJ JonB

              @Ucn_

              webview->page()->runJavaScript("getInfo('" + value1 + "', '" + value2  + "');");
              
              U Offline
              U Offline
              Ucn_
              wrote on last edited by
              #5

              @JonB Thanks.

              JonBJ 1 Reply Last reply
              0
              • U Ucn_

                @JonB Thanks.

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

                @Ucn_
                OK, I believe this is how you do it in nice, readable fashion:

                webview->page()->runJavaScript(QString("getInfo('%1', '%2');").arg(value1).arg(value2));
                

                Looks neater, doesn't it? :)

                To do it properly/robustly, if your values could ever contain a ' character you would have to process your value via a function to protect embedded 's. Also if it has any \s.

                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