我已经开始深入研究C++和Qt,并且一直在使用WebKit Javascript/Qt绑定。我有所有的移动部分工作,除了我的QObject子类在Javascript方面是“未定义”的。下面是我遇到的麻烦简单的测试程序:QWebFrame addToJavaScriptWindowObject()对象在Javascript中未定义
我的主窗口中执行:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// instantiate a webview
QWebView *webview = new QWebView(this);
webview->setGeometry(0, 0, 400, 300);
webview->setUrl(QUrl("file://localhost/Users/kyle/Sites/tests/qt/index.html"));
// instantiate and attach our QObject
hello *h = new hello();
QWebFrame *frame = webview->page()->mainFrame();
frame->addToJavaScriptWindowObject("Hello", h);
// show the window
webview->show();
}
HELLO.CPP
...snip...
QString hello::say()
{
return QString("Kyle");
}
Hello.h
...snip includes...
class hello : public QObject
{
Q_OBJECT
public:
hello();
Q_INVOKABLE QString say();
};
以上这个index.html文件做了一个简单的alert(Hello.say())
调用,但是做了typeof Hello
,我弄不明白。我对C++有点生疏,对Qt也很新颖,所以我确信这是一个noob错误,但我很难过。
您的'attachObject()'公共槽类方法缺少'frame'对象。我通过执行'QWebFrame * frame = ui-> myWebViewWidget-> page() - > mainFrame();'来修复它。除此之外,这正是为我工作的。 – Volomike
这非常适合将我的对象注入到QWebView小部件的mainFrame()中,但似乎无法将其注入到子IFRAME中。你会尝试什么? http://stackoverflow.com/q/33599474/105539 – Volomike