Frage

I'm looking to emit a signal from inside my main window to my WebView.

This one would contain javascript to receive an event, load and insert some html content in it.

I've found this documentation http://developer.nokia.com/community/wiki/Connecting_to_a_QObjects_signal_with_JavaScript_slot_in_Qt_WebKit which explains how to insert the Javascript part. But I don't understand where and how it is connected to the application and therefore how I could do it.

I'm not posting any code since the overall environment is pretty complex, I'm just looking to start a task from the webview with a '<|input type='button'>' which result is too long to compute and display instantly.

I would like to put some loading content waiting to receive the actual one and then pop it up.

War es hilfreich?

Lösung

This is a very good question, I got stuck on it some time! I will show you an example of two way communication: from python to javascript and vice-versa, hope it helps:

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_()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top