首先用QAxWidget打開網頁。在我的另一條文章裏有說明:https://blog.csdn.net/boreassoft/article/details/81005426
QAxObject * parentWindow;
QAxObject * document = this->ui->axWidget->querySubObject("Document");
parentWindow = document->querySubObject("parentWindow");
parentWindow->dynamicCall("execScript(QString,QString)","js代碼","JavaScript");
這樣就可以調用網頁中的js函數了。
需要注意的一點是,如果在網頁載入的語句下面就調用網頁裏的JS函數的語句的話會出現錯誤,因爲頁面沒有加載完畢。這時可以使用一下語句:
parentWindow->dynamicCall("execScript(QString,QString)","window.onload=function(){這裏寫你要調用的JS函數}","JavaScript");
這樣就會默認在頁面加載完成後調用JS函數了。