[Qt-interest] Calling QT objects from script embedded in html pages.
Jean-Michel
jeanmichel.123 at free.fr
Tue May 26 00:18:23 CEST 2009
David Boddie a écrit :
> jeanmichel.123 at free.fr wrote:
>
>
>> Using debug mode, this lead me one step forward, with the error
>> description:
>>
>> message: "Can't find variable: QString"
>> name: "ReferenceError"
>>
>> This occured in the exact following line:
>> var qs = new QString ( 'C:/a.obj' );
>>
>
> This won't work because QString is not exposed to the JavaScript engine in
> the browser. Generally speaking, Qt classes are not accessible from scripts
> running in the browser.
>
>
>> What is the issue?
>> Did I miss some documentation?
>>
>
> It sounds like you need to expose a C++ API to your scripts using the
> QWebFrame::addToJavaScriptWindowObject() function:
>
> http://doc.qtsoftware.com/4.5/qwebframe.html#addToJavaScriptWindowObject
>
> This is how the Form Extractor example works:
>
> http://doc.qtsoftware.com/4.5/webkit-formextractor.html
>
> Take a look at the FormExtractor class to see how it adds itself to the
> script environment:
>
> http://doc.qtsoftware.com/4.5/webkit-formextractor-formextractor-cpp.html
>
> The script itself is in the form.html file included with that example, which
> is unfortunately not online.
>
> David
>
I just tried this QWebFrame::addToJavaScriptWindowObject() .
It does the following job: it allows javascript code, throw the symbol
passed in first argument to access the C++ object passed as second argument.
What I understood, is this mainly mean it allows javascript to call
functions written in C++ as public slots, using their method names, as
function name. Arguments should be of one type of those described in
some documentation page as C++ to javascript or javascript to C++
implicit conversion.
However, I wanted to access not some functions, but an object from
javascript code, in instance, a QFile object.
I also discovered than wit Scriptable object it is (sometimes) possible
to access some engine. With the engine, it is possible to create
functions and constructors and to associate them to a javascript object
to create some class/object like behavior known as prototype. However, I
did not have access to the Javascript engine which run my
WebPage/WebFrame/WebView.
Moreover I did not found the way to store a QFile pointer whithin a
javascript Object, in order to create a QFile like javascript class. (I
use a microsoft-windows environment, which might use version 6 of the OS
provider compiler).
I imagine to create an array of QFile pointers in C++, and have
javascript callable functions which, with a file index argument, will
work on the appropriate QFile. I an not sure of the elegance of this
kind of solution...
More information about the Qt-interest-old
mailing list