Qt函数runJavaScript()不执行JavaScript代码

pra*_*bhu 1 html javascript qt qtwebengine

我正在尝试在Qt中实现网页的显示。我选择使用Qt WebEngine来完成任务。这是我所做的:

  • 编写了一个包含空白表格的示例网页。
  • 仅使用API​​编写JS文件即可在表单内创建单选按钮。

在我的代码中,它看起来像这样:

View = new QWebEngineView(this);
// read the js file using qfile
file.open("path to jsFile");
myJsApi = file.Readall();
View->page()->runjavascript (myjsapi);
View->page()->runjavascript ("createRadioButton(\"button1\");");
Run Code Online (Sandbox Code Playgroud)

我发现该runJavaScript()功能对网页没有影响。我可以在输出窗口中看到该网页,但是我期望的单选按钮不存在。我究竟做错了什么?

IAm*_*PLS 6

我认为你必须将信号连接loadFinished(bool)你的page()一个插槽中,然后执行runJavaScript()此插槽。

void yourClass::mainFunction()
{
    View = new QWebEngineView(this);

    connect( View->page(), SIGNAL(loadFinished(bool)), this, SLOT(slotForRunJS(bool)));
}

void yourClass::slotForRunJS(bool ok)
{
    // read the js file using qfile
    file.open("path to jsFile");
    myJsApi = file.Readall();
    View->page()->runJavaScript(myjsapi);
    View->page()->runJavaScript("createRadioButton(\"button1\");");
}
Run Code Online (Sandbox Code Playgroud)