2016-06-27 81 views
0

我想實現在Qt中顯示的網頁。我選擇使用Qt WebEngine來完成我的任務。以下是我所做的:Qt函數runJavaScript()不執行JavaScript代碼

  • 寫了一個由空格式組成的示例網頁。
  • 用一個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\");"); 

我發現runJavaScript()函數的網頁上沒有任何影響。我可以在輸出窗口中看到網頁,但我預期的單選按鈕不存在。我究竟做錯了什麼?

回答

3

我想你必須將你的page()的信號loadFinished(bool)連接到一個插槽,然後在這個插槽中執行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\");"); 
} 
+0

哦..感謝一噸。這確實工作! – prabhu

+0

很高興聽到它!如果它適合你,隨時接受答案,因爲它可以幫助其他人。 – IAmInPLS