无法使用 QtWebkit 通过 javascript 访问 Qt 对象方法
使用 QtWebkit 的 javascript 桥,我创建了一个类来将 Web 框架中的数据与 Qt 代码的其余部分连接起来。它识别该对象,但不识别其方法。
//executed with main window constructor
void MainWindow::initJavascriptInterface(){
webInterfacer JSInterface;
QWebFrame *frame = ui->webView->page()->mainFrame();
frame->addToJavaScriptWindowObject("sender",&JSInterface);
}
该类只有一个名为 getInfo() 的公共槽(我知道这个名字很糟糕,但它是用于测试的,我尝试过使用不同的函数名称,以防发生冲突)。
webinterfacer.h
#ifndef WEBINTERFACER_H
#define WEBINTERFACER_H
#include <QObject>
class webInterfacer : public QObject
{
Q_OBJECT
public:
explicit webInterfacer();
~webInterfacer();
public slots:
void getInfo();
signals:
//signal to emit when getInfo is called
void openPopup(QString,QString);
};
#endif // WEBINTERFACER_H
我尝试在 getInfo 中使用不同的内容,即使是空函数,它也永远不会被识别。
在我的 HTML 标头 (jQuery) 中:
$(document).ready(function(){
if(window.sender){
alert("obj: " + typeof sender); //shows "obj: object"
alert("getInfo: " + typeof sender.getInfo); //shows "getInfo: undefined"
}
}
Using QtWebkit's javascript bridge, I have created a class to interface the data in my web frame with the rest of my Qt code. it recognises the object, but none of its methods.
//executed with main window constructor
void MainWindow::initJavascriptInterface(){
webInterfacer JSInterface;
QWebFrame *frame = ui->webView->page()->mainFrame();
frame->addToJavaScriptWindowObject("sender",&JSInterface);
}
The class has only one public slot called getInfo() (I know the name sucks but it's for testing, i have tried with different function names in case it was a collision).
webinterfacer.h
#ifndef WEBINTERFACER_H
#define WEBINTERFACER_H
#include <QObject>
class webInterfacer : public QObject
{
Q_OBJECT
public:
explicit webInterfacer();
~webInterfacer();
public slots:
void getInfo();
signals:
//signal to emit when getInfo is called
void openPopup(QString,QString);
};
#endif // WEBINTERFACER_H
I tried with different content in getInfo, even an empty function, it is never recognized.
In my HTML header (jQuery):
$(document).ready(function(){
if(window.sender){
alert("obj: " + typeof sender); //shows "obj: object"
alert("getInfo: " + typeof sender.getInfo); //shows "getInfo: undefined"
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
webInterFacer JSInterface;
你的 webInterfacer 对象是一个局部变量。一旦超出范围,即退出函数 initJavaScriptInterface(); ,它就会被销毁。
使固定
}
webInterFacer JSInterface;
Your webInterfacer object is a local variable.It is destroyed as soon as it is out of scope ie, once you exit the function initJavaScriptInterface();
Fix
}