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.3k 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 13 Dec 2019, 17:54 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

    J 1 Reply Last reply 13 Dec 2019, 18:17
    0
    • U Ucn_
      13 Dec 2019, 18:33

      @JonB Thanks, can that method pass two parameters?

      J Offline
      J Offline
      JonB
      wrote on 13 Dec 2019, 18:59 last edited by
      #4

      @Ucn_

      webview->page()->runJavaScript("getInfo('" + value1 + "', '" + value2  + "');");
      
      U 1 Reply Last reply 13 Dec 2019, 19:05
      1
      • U Ucn_
        13 Dec 2019, 17:54

        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

        J Offline
        J Offline
        JonB
        wrote on 13 Dec 2019, 18:17 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 13 Dec 2019, 18:33
        1
        • J JonB
          13 Dec 2019, 18:17

          @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 13 Dec 2019, 18:33 last edited by
          #3

          @JonB Thanks, can that method pass two parameters?

          J 1 Reply Last reply 13 Dec 2019, 18:59
          0
          • U Ucn_
            13 Dec 2019, 18:33

            @JonB Thanks, can that method pass two parameters?

            J Offline
            J Offline
            JonB
            wrote on 13 Dec 2019, 18:59 last edited by
            #4

            @Ucn_

            webview->page()->runJavaScript("getInfo('" + value1 + "', '" + value2  + "');");
            
            U 1 Reply Last reply 13 Dec 2019, 19:05
            1
            • J JonB
              13 Dec 2019, 18:59

              @Ucn_

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

              @JonB Thanks.

              J 1 Reply Last reply 13 Dec 2019, 19:10
              0
              • U Ucn_
                13 Dec 2019, 19:05

                @JonB Thanks.

                J Offline
                J Offline
                JonB
                wrote on 13 Dec 2019, 19:10 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

                1/6

                13 Dec 2019, 17:54

                • 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