public
Last active

PySide/WebKit/Javascript

  • Download Gist
pyside_webkit_javascript.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
import sys
from PySide.QtCore import QObject, Slot
from PySide.QtGui import QApplication
from PySide.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)
 
@Slot(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_()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.