Skip to content

Instantly share code, notes, and snippets.

@yorikvanhavre
Created April 9, 2012 00:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorikvanhavre/2340514 to your computer and use it in GitHub Desktop.
Save yorikvanhavre/2340514 to your computer and use it in GitHub Desktop.
call pyqt4 stuff from javascript
import sys
from PyQt4 import QtCore, QtGui, QtWebKit
import WebGui
"""Html snippet."""
html = """
<html><body>
<center>
<script language="JavaScript">
document.write('<p>Python ' + pyObj.pyVersion + '</p>')
</script>
<button onClick="pyObj.showMessage('Hello from WebKit')">Press me</button>
</center>
</body></html>
"""
class StupidClass(QtCore.QObject):
"""Simple class with one slot and one read-only property."""
@QtCore.pyqtSlot(str)
def showMessage(self, msg):
"""Open a message box and display the specified message."""
QtGui.QMessageBox.information(None, "Info", msg)
def _pyVersion(self):
"""Return the Python version."""
return sys.version
"""Python interpreter version property."""
pyVersion = QtCore.pyqtProperty(str, fget=_pyVersion)
myObj = StupidClass()
a = QtGui.qApp
mw = a.activeWindow()
v= mw.findChild(QtWebKit.QWebFrame)
#v.page().currentFrame().addToJavaScriptWindowObject('pyObj', myObj)
#v.addToJavaScriptWindowObject('pyObj', myObj)
v.setHtml(html)
curr1 = v.page()
frame1 = curr1.currentFrame()
frame1.addToJavaScriptWindowObject("pyObj", myObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment