[Qt-interest] addToJavaScriptWindowObject for JS in HTML?

Paul Colby qt at colby.id.au
Mon Oct 12 08:45:10 CEST 2009


I can't figure this out... there must be something I'm missing.

I've used QWebFrame::addToJavaScriptWindowObject quite a bit, and find
it very useful.  But now I'm trying to use it slightly differently,
but to my surprise I cannot get it to work.

I have a basic QObject-derived class that implements a single
read-only Q_PROPERTY called "version".

So if I expose that object under the name "myObject", then this works:

QVariant result=webView.page()->mainFrame()->evaluateJavaScript(QString::fromAscii("myObject.version"));

However, if I load a HTML page that contains the following JavaScript:

alert(myObject); // null.
alert(document.getElementById("myObject")); // undefined.

I get "null" and "undefined" respectively.  Should this be the case?

I know about QWebFrame::javaScriptWindowObjectCleared - I'm calling
addToJavaScriptWindowObject there, and in about 20 other places /
signals just be sure... but that none of them help :(

Also, I've overridden QWebPage::javaScriptAlert, and there, as the
"null" and "undefined" messages are coming though, I can see that the
associated QWebFrame parameter *does* has have my object (ie
evaluateJavaScript finds it just fine).

So, am I missing something?  Is HTML-embedded JavaScript meant to be
able to see objects exposed by addToJavaScriptWindowObject?  or are
they only meant to be visible to explicit evaluateJavaScript calls?

Thanks :)

paul
-- 
http://colby.id.au



More information about the Qt-interest-old mailing list