Kyl*_*yle 2 javascript qt qwebkit
我已经开始再次深入研究C++和Qt,并且一直在使用WebKit Javascript/Qt绑定.我的所有移动部件都在工作,但我的QObject子类在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();
}
Run Code Online (Sandbox Code Playgroud)
HELLO.CPP
...snip...
QString hello::say()
{
return QString("Kyle");
}
Run Code Online (Sandbox Code Playgroud)
Hello.h
...snip includes...
class hello : public QObject
{
Q_OBJECT
public:
hello();
Q_INVOKABLE QString say();
};
Run Code Online (Sandbox Code Playgroud)
上面提到的index.html文件做了一个简单的alert(Hello.say())调用,但是这样做typeof Hello,我得到了未定义.
我对C++有点生疏,对Qt来说很新,所以我确定这是一个noob错误,但我很难过.
无法随时在页面中插入对象.你应该把那一行:
frame->addToJavaScriptWindowObject("Hello", h);
Run Code Online (Sandbox Code Playgroud)
在连接到javaScriptWindowObjectCleared()信号的插槽中QWebFrame并移动一些代码,因此您可以从该插槽访问该帧.
另请参阅Qt附带的Form Extractor示例.
| 归档时间: |
|
| 查看次数: |
6996 次 |
| 最近记录: |