PyQt (PySide), WebKit and exposing methods from/to

2019-03-11 06:57发布

问题:

I am planning to use PyQt to control an embedded WebKit browser on the server side.

I have some inherit application logic in Javascript in the HTML page running inside WebKit.

How could I communicate from the host process (Python, PyQt) with Javascript, so that

  • I can call Javascript functions inside the page

  • Python methods are exposed to Javascript and can be called from the Javascript, with arguments

回答1:

The following source code should be helpful:

import sys
from PyQt4.QtCore import QObject, pyqtSlot
from PyQt4.QtGui import QApplication
from PyQt4.QtWebKit import QWebView

html = """
<html>
<body>
    <h1>Hello!</h1><br>
    <h2><a href="#" onclick="printer.text('Message from QWebView')">QObject Test</a></h2>
    <h2><a href="#" onclick="alert('Javascript works!')">JS test</a></h2>
</body>
</html>
"""

class ConsolePrinter(QObject):
    def __init__(self, parent=None):
        super(ConsolePrinter, self).__init__(parent)

    @pyqtSlot(str)
    def text(self, message):
        print message

if __name__ == '__main__':
    app = QApplication(sys.argv)
    view = QWebView()
    frame = view.page().mainFrame()
    printer = ConsolePrinter()
    view.setHtml(html)
    frame.addToJavaScriptWindowObject('printer', printer)
    frame.evaluateJavaScript("alert('Hello');")
    frame.evaluateJavaScript("printer.text('Goooooooooo!');")
    view.show()
    app.exec_()