Results 1 to 10 of 10

Thread: Call a JavaScript function from C++ using QtWebkit

  1. #1
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Call a JavaScript function from C++ using QtWebkit

    Hi all:

    Some time ago I developed a browser using WebkitGTK, and added some extra javascript objects to implement a framework I needed for a project.

    Now I need to rewrite this browser using QtWebKit. I've been successful in calling C++ methods from JavaScript, but now I need to register a JavaScript callback, and be able to call it from C++. In the original system I built a C function which received an object as parameter (using JSValueToObject to convert the value received by the function to an object). That object was the JavaScript function to be called as callback. An example: this Javascript code registers the JavaScript callback called cb_function:

    Qt Code:
    1. bool cb_function(param1, param2) {
    2.  
    3. alert("This is the callback, called with "+param1);
    4.  
    5. }
    6.  
    7. my_c_class.set_callback(cb_function);
    To copy to clipboard, switch view to plain text mode 

    Here, I define the cb_function callback, and send it as a parameter to the underlying framework. Then, in C, I used JSObjectCallAsFunction() to call the JavaScript function whenever I needed, sending to it some parameters.

    What I need to know is how to do this with QtWebKit instead. I presume that the first part needs to use a Q_INVOKABLE method (called, in this case, set_callback) which receives the function. The problem is which type to use. If I put a String, I receive a text transcript of the function itself, not an object that I can call. Should it need to be a QVariant or a QObject?

    And now the second part: how can I call from C++ that function I received?

    Thanks!!!

  2. #2
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Qt Code:
    1. QWebView::page()->mainFrame()->evaluateJavaScript( "some_js_function()" );
    To copy to clipboard, switch view to plain text mode 
    should do the trick.

  3. #3
    Join Date
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Unfortunately that's not a solution, because the callback can be inside an iFrame. In that case, your solution won't work

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

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Quote Originally Posted by Rastersoft View Post
    Unfortunately that's not a solution, because the callback can be inside an iFrame. In that case, your solution won't work
    How would you implement it from within the HTML page code?
    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
    Mar 2012
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Inside the iFrame is loaded a page that has

    Qt Code:
    1. bool cb_function(param1, param2) {
    2.  
    3. alert("This is the callback, called with "+param1);
    4.  
    5. }
    6.  
    7. my_c_class.set_callback(cb_function);
    To copy to clipboard, switch view to plain text mode 

    If, now, from the C++ code I receive this as a string, it will contain "bool cb_function...". If I use that way, I would be calling "cb_function" in the main page, not inside the iFrame.

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

    Default Re: Call a JavaScript function from C++ using QtWebkit

    This is a trivial case where the callback is registered in the same place where the function is called, I doubt you really have a case like that. From what I understand an equivalent would be to register a callback from the main page to a function that lives in some subframe, right? So how would you do that in pure javascript?
    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.


  7. #7
    Join Date
    Sep 2011
    Location
    Manchester
    Posts
    538
    Thanks
    3
    Thanked 106 Times in 103 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    You didn't answer wysota's question.

    How do you call function inside iFrame from the parent document?
    If you can answer this question - you will answer yourself how to call it from c as the approach is the same.

    As an alternative you can try QWebPage::currentFrame() or QWebPage::frameAt().

    If you don't know how to call js inside iFrame from parent document consider this:
    Qt Code:
    1. QWebView::page()->mainFrame()->evaluateJavaScript( "function call_iFrame() { document.getElementById('iframe_id').contentWindow.method_inside_iframe(); }" )
    To copy to clipboard, switch view to plain text mode 
    That should get you going.

  8. #8
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Quote Originally Posted by Spitfire View Post
    As an alternative you can try QWebPage::currentFrame() or QWebPage::frameAt().

    If you don't know how to call js inside iFrame from parent document consider this:
    Qt Code:
    1. QWebView::page()->mainFrame()->evaluateJavaScript( "function call_iFrame() { document.getElementById('iframe_id').contentWindow.method_inside_iframe(); }" )
    To copy to clipboard, switch view to plain text mode 
    That should get you going.
    I am sorry I am resurrecting this topic, but following it I think this topic is incomplete and my question might start from this point.
    Into an application that use a WebView I am loading some web content. Trying to execute a JavaScript code that call a alert() window directly into evaluateJavaScript() is fine, but calling it into a function it fails.
    Qt Code:
    1. QString strTest1 ("alert(\"this is sly!\"); null ");
    2. QVariant f1result = webView->page()->mainFrame()->evaluateJavaScript( strTest1 ); // fine
    3. QString strTest2 ("function alert_something() { alert(\"this is sly!\"); } ");
    4. QVariant f2result = webView->page()->mainFrame()->evaluateJavaScript( strTest2 ); // bad
    5.  
    6. qDebug() << f2result.toString(); // returns nothing
    To copy to clipboard, switch view to plain text mode 
    I can't figure why calling that trivial function call fails... Ideas?
    Last edited by SlyMaximus; 13th May 2014 at 22:54.

  9. #9
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Your second JavaScript snippet only contains a function definition, not a call to that function.

    Cheers,
    _

  10. The following user says thank you to anda_skoa for this useful post:

    SlyMaximus (14th May 2014)

  11. #10
    Join Date
    May 2014
    Posts
    2
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Call a JavaScript function from C++ using QtWebkit

    Quote Originally Posted by anda_skoa View Post
    Your second JavaScript snippet only contains a function definition, not a call to that function.

    Cheers,
    _
    Loolll! I think I was blind, of-course we need that call.
    10x!

Similar Threads

  1. QTWebKit, out of control Javascript CPU usage
    By Dkamerad in forum Qt Programming
    Replies: 2
    Last Post: 12th December 2014, 06:01
  2. [QtWebKit] Inject JavaScript properly when DOM loads..
    By fab_74 in forum Qt Programming
    Replies: 2
    Last Post: 27th March 2011, 18:58
  3. Replies: 2
    Last Post: 15th November 2010, 06:17
  4. QtWebkit and JavaScript error messages
    By Diph in forum Qt Programming
    Replies: 1
    Last Post: 29th April 2009, 12:10
  5. add custom HTML attribute by javascript in QtWebKit
    By sand.fj.wen in forum Qt Programming
    Replies: 3
    Last Post: 17th April 2009, 08:25

Tags for this Thread

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.