Results 1 to 6 of 6

Thread: Call JavaScript function with parameters from Qt C++

  1. #1
    Join Date
    May 2012
    Posts
    99
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question Call JavaScript function with parameters from Qt C++

    I know how can I call a JavaScript function without parameters from Qt C++ code:

    JavaScript code:

    Qt Code:
    1. function foo() {
    2. console.log("bar");
    3. }
    To copy to clipboard, switch view to plain text mode 

    Qt code:

    Qt Code:
    1. QString foo = "foo(); null";
    2. ui->webView->page()->mainFrame()->evaluateJavaScript(foo);
    To copy to clipboard, switch view to plain text mode 

    I want to call a JavaScript fuction with parameters from Qt C++ code. How can I do it? It's possible to perform a pass parameters?

    Best regards.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,420
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call JavaScript function with parameters from Qt C++

    If the parameter value can be expressed as a string, you could simple make it part of the "script"

    Qt Code:
    1. const QString scriptWithPlaceHolders = "foo(%1);";
    2.  
    3. evaluateJavaScrpt(scriptWithPlaceHolders.arg(argument));
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. #3
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call JavaScript function with parameters from Qt C++

    Im having some problems with this.
    Ive got this code
    Qt Code:
    1. const QString scriptWithPlaceHolders = "test();null";
    2. ((std::pair<QWebView*, std::string>)publicWindows.at(0)).first->page()->mainFrame()->evaluateJavaScript(scriptWithPlaceHolders);
    To copy to clipboard, switch view to plain text mode 
    over this javascript
    Qt Code:
    1. function test(){
    2. document.getElementById("title").innerHTML = ":(";
    3. }
    To copy to clipboard, switch view to plain text mode 

    And everything seems fine, but if i try:

    Qt Code:
    1. const QString scriptWithPlaceHolders = "test(%1);null";
    2. ((std::pair<QWebView*, std::string>)publicWindows.at(0)).first->page()->mainFrame()->evaluateJavaScript(scriptWithPlaceHolders.arg(":("));
    To copy to clipboard, switch view to plain text mode 
    over

    Qt Code:
    1. function test(purq){
    2. document.getElementById("title").innerHTML = ":(";
    3. }
    To copy to clipboard, switch view to plain text mode 

    it does not work, any idea on what can be wrong?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,363
    Thanks
    3
    Thanked 5,012 Times in 4,791 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Call JavaScript function with parameters from Qt C++

    Please explain how "does not work" manifest itself.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Call JavaScript function with parameters from Qt C++

    If I were a betting man, I would bet you need to include some escaped quotations so that your parameter gets passed as a string to your function.

  6. #6
    Join Date
    Mar 2014
    Posts
    9
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call JavaScript function with parameters from Qt C++

    does not work was function not being called, but sulliwk was right it worked doing this:
    evaluateJavaScript(scriptWithPlaceHolders.arg("''"));
    Some wasted hours blaming everything else xD. But i really cant understand why is that even needed, more when thinking on javascript and his weak typing.
    Thank you dudes.

Similar Threads

  1. Call a JavaScript function from C++ using QtWebkit
    By Rastersoft in forum Qt Programming
    Replies: 9
    Last Post: 14th May 2014, 13:03
  2. Replies: 3
    Last Post: 17th October 2013, 09:12
  3. Replies: 4
    Last Post: 2nd August 2012, 08:42
  4. Replies: 2
    Last Post: 15th November 2010, 06:17
  5. QScript + get function call parameters
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 1st August 2009, 16:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.