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"函数从 c++ 在 JavaScript 中触发.另外我事先不知道函数指针的名称.
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:
初始化网页视图:
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:
<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屋!