2014-02-11 63 views
1

我期待從我的主窗口發出一個信號到我的WebView。如何從應用程序向PyQtWebkitView javascript發送信號?

這一個將包含JavaScript來接收事件,加載並插入一些html內容。

我發現這個文檔http://developer.nokia.com/community/wiki/Connecting_to_a_QObjects_signal_with_JavaScript_slot_in_Qt_WebKit,它解釋瞭如何插入javascript部分。但我不明白它在哪裏以及如何連接到應用程序,因此我怎麼能做到這一點。

我沒有發佈任何代碼,因爲整體環境相當複雜,我只是想從webview開始一個'< | input type ='button'>'任務,結果太長立即計算並顯示。

我想把一些加載內容等待接收實際的一個,然後彈出它。

回答

2

這是一個非常好的問題,我被卡住了一段時間!我會告訴你雙向通信的一個例子:從蟒蛇到JavaScript和反之亦然,希望它有助於:

import PyQt4.QtGui as gui, PyQt4.QtWebKit as web, PyQt4.QtCore as core 

class MyMainWindow(gui.QMainWindow):  

    proccessFinished = core.pyqtSignal() 

    def __init__(self, parent=None): 
     super(MyMainWindow,self).__init__() 

     self.wv = web.QWebView() 
     self.setCentralWidget(self.wv) 

     #pass this main window to javascrip 
     self.wv.page().mainFrame().addToJavaScriptWindowObject("mw", self)   

     self.wv.setHtml(""" 
     <html> 
     <head> 
      <script language="JavaScript"> 
       function p() { 
        document.write('Process Finished') 
       } 
       mw.proccessFinished.connect(p)     
      </script> 
     </head> 
     <body> 
      <h1>It works</h1> 
      <input type=button value=click onClick=mw.doIt()></input> 
     </body> 
     </html> 
     """) 

    @core.pyqtSlot() 
    def doIt(self): 
     print('running a long process...') 
     print('of course it should be on a thread...') 
     print('and the signal should be emmited from there...') 
     self.proccessFinished.emit() 


app = gui.QApplication([]) 

mw = MyMainWindow() 
mw.show() 

app.exec_() 
+0

當調用addToJavaScriptWindowObject你路過它有實現的方法主要窗口對象。我怎樣才能將插槽方法存儲在其他類中? (我的應用程序是基於插件:)) – cp151

相關問題