简体   繁体   English

无法从QWebFrame / QWebView调用Javascript函数

[英]Javascript function not being called from QWebFrame/QWebView

Can someone please explain why my JavaScript function is not being called. 有人可以解释为什么我的JavaScript函数没有被调用。

The JS function is: JS函数是:

function loadKMLFile(kmlUrl)
{
    alert('in loadKMLFile().');
    google.earth.fetchKml(ge, kmlUrl, finishFetchKml);
}

My C++ Qt code is: 我的C ++ Qt代码是:

void MainWindow::on_loadKML_button_clicked()
{
    QString fileUrl = ui->textEdit->toPlainText(); // Get path to file
    ui->webView->page()->mainFrame()->evaluateJavaScript("loadKMLFile(" + fileUrl + ")");
}

When I run my app, I enter in a path to a file to load. 运行我的应用程序时,我输入要加载的文件的路径。 This is then extracted by the C++ method above and a call made via evaluateJavaScript passing in the path. 然后,通过上面的C ++方法将其提取,并通过在路径中传递的validateJavaScript进行调用。

If the path passed in is empty string, the JS function is called and the alert box appears. 如果传入的路径为空字符串,则会调用JS函数,并显示警告框。 If a put a single character (let alone the whole path) in the text edit, the function is not called I do not get the alert box appear. 如果在文本编辑中放置单个字符(更不用说整个路径),则该函数不被调用,但不会出现提示框。

Any ideas would be gratefully received. 任何想法将不胜感激。

did you enable javascript already? 您已经启用javascript吗?

webView->page()->settings()->testAttribute( QWebSettings::JavascriptEnabled );

refer: qwebsettings WebAttribute 请参阅: qwebsettings WebAttribute

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM