#!/usr/bin/env python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *


class PythonJS(QObject):

    @pyqtSignature("QString")
    def alert(self, msg):
        QMessageBox.information(None, "This is Python", msg)

    @pyqtSignature("", result="int")
    def fortune(self):
        return 23


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)

    browser = QWebView()
    browser.show()

    browser.setHtml("""
       <script>function fortune() { return 42; }</script>
       <h1>Hello, world</h1>
       <input type="button" value="Click JavaScript!" onClick="alert('hi ' + fortune())"/>
       <input type="button" value="Click Python!" onClick="python.alert('hi ' + python.fortune())"/>
    """)

    frame = browser.page().mainFrame()
    frame.addToJavaScriptWindowObject("python", PythonJS())

    app.exec_()
