Skip to content

Instantly share code, notes, and snippets.

@matthewryanscott
Created December 2, 2009 02:01
Show Gist options
  • Save matthewryanscott/246859 to your computer and use it in GitHub Desktop.
Save matthewryanscott/246859 to your computer and use it in GitHub Desktop.
talking between qt widget and javascript
import os
import sys
from PyQt4 import uic
from PyQt4.QtCore import QSize, Qt, QTimer, pyqtProperty, pyqtSignal, pyqtSlot
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
html = \
"""<html>
<head>
<title>Python Web Plugin Test</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.pack.js"></script>
<script type="text/javascript">
var f;
$(document).ready(function() {
// Grab the qt form.
f = $('#form1')[0];
var on_timeout = function () {
console.log('timeout triggered');
};
// On html button click, add text.
$('#button').click(function () {
f.timeout.connect(on_timeout);
f.setLabelText('CLICKED!');
f.setLineEditText(f.lineEditText() + ' xyz');
console.log('html button clicked');
});
});
</script>
</head>
<body>
<h1>Python Web Plugin Test</h1>
<object type="x-pyqt/form1" width="200" height="200" id="form1"></object>
<p>This is a Web plugin written in Python.</p>
<button id="button">lowercase</button>
</body>
</html>
"""
ui_filename = os.path.join(os.path.dirname(__file__), 'proto.ui')
FormClass, QtBaseClass = uic.loadUiType(ui_filename)
class WebWidget(FormClass, QtBaseClass):
timeout = pyqtSignal()
def __init__(self, parent=None):
QtBaseClass.__init__(self, parent)
self.setupUi(self)
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.start()
self.timer.timeout.connect(self.on_timeout)
def on_timeout(self):
self.timeout.emit()
@pyqtSlot()
def labelText(self):
return self.label.text()
@pyqtSlot(unicode)
def setLabelText(self, s):
self.label.setText(s)
@pyqtSlot(result=unicode)
def lineEditText(self):
s = self.lineEdit.text()
return s
@pyqtSlot(unicode)
def setLineEditText(self, s):
self.lineEdit.setText(s)
def sizeHint(self):
return QSize(100, 100)
class WebPluginFactory(QWebPluginFactory):
def __init__(self, parent = None):
QWebPluginFactory.__init__(self, parent)
def create(self, mimeType, url, names, values):
if mimeType == "x-pyqt/form1":
return WebWidget()
def plugins(self):
plugin = QWebPluginFactory.Plugin()
plugin.name = "PyQt Widget"
plugin.description = "An example Web plugin written with PyQt."
mimeType = QWebPluginFactory.MimeType()
mimeType.name = "x-pyqt/form1"
mimeType.description = "PyQt widget"
mimeType.fileExtensions = []
plugin.mimeTypes = [mimeType]
return [plugin]
if __name__ == "__main__":
app = QApplication(sys.argv)
QWebSettings.globalSettings().setAttribute(
QWebSettings.PluginsEnabled, True)
QWebSettings.globalSettings().setAttribute(
QWebSettings.DeveloperExtrasEnabled, True)
view = QWebView()
factory = WebPluginFactory()
view.page().setPluginFactory(factory)
view.setHtml(html)
view.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment