admin管理员组文章数量:1318563
Is there any way I can expose a C++ object/function to JavaScript running inside the QtWebKit browser in Qt? It's possible to expose ActionScript objects to JS code running inside the WebKit browser in Adobe AIR - I'm looking for similar functionality in Qt.
Is there any way I can expose a C++ object/function to JavaScript running inside the QtWebKit browser in Qt? It's possible to expose ActionScript objects to JS code running inside the WebKit browser in Adobe AIR - I'm looking for similar functionality in Qt.
Share Improve this question edited Apr 26, 2010 at 12:49 Björn Pollex 76.9k29 gold badges205 silver badges289 bronze badges asked Apr 26, 2010 at 12:16 BlueSilverBlueSilver 1,0522 gold badges10 silver badges25 bronze badges2 Answers
Reset to default 4EDIT: LINK IS WORKING, BUT JUST IN CASE THE LINK BREAKS AGAIN [Link] by Richard Moore ...
One question I've seen e up several times on #qt and qt-interest is how to add custom (application specific) APIs to those available by default in QtWebKit. This is actually pretty easy (once you know how) as I'll show below. This post will show a simple example of how to make an object available from javascript, including calling methods on the object and returning values from C++.
There are two things you really need to know in order to perform this integration, the first is the addToJavaScriptWindowObject() method of QWebFrame, this allows will make the specified QObject visible from javascript. The second thing you need to know is that objects published in this way will vanish each time the javascript window object is cleared - ie. every time the user navigates to a new page. To prevent this from causing problems, QWebFrame provides a signal that tells you whenever the object is cleared allowing you to re-add your custom API. This is actually much simpler than it sounds!
The core of this is really implemented in two methods in the example, they're shown below:
void MyApi::setWebView( QWebView *view )
{
QWebPage *page = view->page();
frame = page->mainFrame();
attachObject();
connect( frame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(attachObject()) );
}
void MyApi::attachObject()
{
frame->addToJavaScriptWindowObject( QString("MyApi"), this );
}
This code is all that you need in order to make all of the public slots of the MyApi object visible to javascript. The MyApi class provides two public slots:
public slots:
void doSomething( const QString ¶m );
int doSums( int a, int b );
The first slot simply logs a message to the debug output, the second returns the sum of its two arguments (yes, slots can return things!). They're called from javascript like this:
MyApi.doSomething( 'Hello from JS page 2!!!!' );
sum = MyApi.doSums( 2, 3 );
alert( 'C++ says the sum is ' + sum );
And that's all there is to it! You can download the code from >http://xmelegance/devel/qtwebkitextension.tar.gz.
Yes. Take a look at this. It should be a good start.
本文标签: Expose C object to Javascript in QtStack Overflow
版权声明:本文标题:Expose C++ object to Javascript in Qt - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742049389a2417982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论