[Qt-interest] javascript does not load in QWebview
Tony Rietwyk
tony.rietwyk at rightsoft.com.au
Sat Mar 12 02:03:03 CET 2011
Hi,
Best to respond to the group, not me personally!
I'm sure you will need to use the loadFinished slot. In my case, I have
Javascript executing code in my C++ object, not C++ executing Javascript as
you require.
Your abc() function should work. As others have suggested, adding an
alert() will help you know whether it is being called. Have you tested
executing your Javascript functions in the onLoad event?
Your getroute function probably won't work - you are passing a function into
the google api to be executed when it has finished communicating with the
network. So you should not expect the address variable to be filled in when
you call getroute! I suggest to have a look at the Qt docs for
addToJavaScriptWindowObject - pass a QObject based object to the Javascript
environment, and then call one of its slots in the anonymous function that
you pass to the Google api.
Tony.
-----Original Message-----
From: Gopalakrishna Bhat [mailto:gopalakbhat at gmail.com]
Sent: Friday, 11 March 2011 14:15
To: Tony Rietwyk
Subject: Re: [Qt-interest] javascript does not load in QWebview
Hi Tony,
I too thought the same thing, but that is not the reason for the method not
being called. I had tried the loadFinshed signal myself and it did not work.
With regards,
Gopalakrishna Bhat
On Thu, Mar 10, 2011 at 4:50 PM, Tony Rietwyk
<tony.rietwyk at rightsoft.com.au> wrote:
Hi Karthick,
I don't think QWebView.load is synchronous - although the 4.6.0 docs doesn't
explicitly say either way! You need to let the event loop spin for the view
to access the network, etc.
I suggest to add a slot and connect to the view's loadFinished(). Then you
can check whether the load worked, and evaluate your Javascript.
Hope that helps,
Tony.
Sent: Thursday, 10 March 2011 14:49
Hi All,
I am trying to execute the javascript through evaluate javascript() call in
QWebview. while calling evaluate javacript() call in Qt, the returned value
from that call is null. how to make javascript works in Qwebview?
please help me to solve this issue?
Qt code:
QWebView *view;
view->load(QUrl("file:///home/admin/test.html"));
Qvariant name, address;
name = view->page()->mainFrame()->evaluateJavaScript("abc()");
qDebug() << name.toString(); //getting null value
address = view->page()->mainFrame()->evaluateJavaScript("getroute()");
qDebug() << address.toString(); //getting null value
test.html:
<html>
<head>
<script type="text/javascript"
src="http://maps.google.com/maps/api/js?sensor=false"></script> //This is
for calling google maps api's
<script type="text/javascript">
function abc()
{
return "SUCCESS";
}
var address; //global variable
function getroute()
{
/*
declaration and initialization of google maps api stuff
*/
calling google api directionsService.route(req, function(resp,status){
if (status == ok)
{
address = resp.routes[0].legs[0].start_address;
} });
return address;
}
</script>
<body>
</body>
</html>
--
Regards,
Karthick
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110312/0e81319f/attachment.html
More information about the Qt-interest-old
mailing list