Qt QWEBview JavaScript回调 [英] Qt QWEBview JavaScript callback
问题描述
如何将函数指针从JavaScript传递到插槽?
How to pass a function "pointer" from JavaScript to a slot?
在JavaScript中:
in JavaScript:
function f1()
{
alert("f1");
}
qtclass.submit(f1);
和Qt:
public slots:
void submit(void * ptr)
{
(void)ptr;
}
我需要f1函数来解决JavaScript中的问题c ++,一旦完成一些处理。另外我事先并不知道函数指针的名称。
I need the "f1", function to get fired in the JavaScript from the c++, once some processing is done. Also I do not know in advance the name of the function pointer.
推荐答案
你应该能够使用 QWebFrame :: evaluateJavaScript 方法。看看下面的示例是否适合您:
you should be able to execute your script using QWebFrame::evaluateJavaScript method. See if an example below would work for you:
初始化webview:
initializing webview:
QWebView *view = new QWebView(this->centralWidget());
view->load(QUrl("file:///home//test.html"));
connect(view, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));
loadFinished信号处理程序:
loadFinished signal handler:
void MyTestWindow::loadFinished(bool)
{
QVariant f1result = ((QWebView*)sender())->page()->mainFrame()->evaluateJavaScript("f1('test param')");
qDebug() << f1result.toString();
}
test.html:
test.html:
<head>
<script LANGUAGE="JavaScript">
function f1 (s)
{
alert (s)
return "f1 result"
}
</script>
</head>
<body>
test html
</body>
evaluateJavaScript 应触发警告消息框并返回带有f1函数结果的QVariant 。
evaluateJavaScript should trigger an alert message box and return QVariant with f1 function result.
希望这有帮助,尊重
这篇关于Qt QWEBview JavaScript回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!