我提供一下我成功執行javascript的方法,希望對你有幫助。
1。在QWebView子類的建構式中先把所需要的html檔讀入
QFile file(":/Resources/xxx.html");
if (file.open(QIODevice::ReadOnly)){
QByteArray html = file.readAll();
file.close();
this->setHtml(html);
}
2。並且在load完html後利用自動產生訊號的loadFinished(bool)來呼叫我的function finished(bool)
connect(this, SIGNAL(loadFinished(bool)), this, SLOT(finished(bool)));
3。在finished(bool) function中呼叫javascript函式
if (ok){
QWebFrame *frame = this->page()->currentFrame();
frame->evaluateJavaScript("函式名()");
}