Skip to content

Instantly share code, notes, and snippets.

@fmarani
Created December 5, 2014 08:48
Show Gist options
  • Save fmarani/a366b8409e4f376f3049 to your computer and use it in GitHub Desktop.
Save fmarani/a366b8409e4f376f3049 to your computer and use it in GitHub Desktop.
python webkit integration test - with inspector
import sys
from PySide.QtCore import QObject, Slot, QTimer, Qt
from PySide.QtGui import QApplication, QWidget, QSplitter, QVBoxLayout, QShortcut, QKeySequence
from PySide.QtWebKit import QWebView, QWebSettings, QWebInspector
html = """
<html>
<body>
<h1>Hello!</h1><br>
<div id="box"></div>
<input type="text" id="text">
<h2><a href="#" onclick="printer.text(document.getElementById('text').value)">QObject Test</a></h2>
<h2><a href="#" onclick="alert('Javascript works!')">JS test</a></h2>
<h2><a href="#" onclick="blabla('Javascript works!')">JS broken 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
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
self.view = QWebView(self)
self.setupInspector()
self.splitter = QSplitter(self)
self.splitter.setOrientation(Qt.Vertical)
layout = QVBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(self.splitter)
self.splitter.addWidget(self.view)
self.splitter.addWidget(self.webInspector)
def setupInspector(self):
page = self.view.page()
page.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
self.webInspector = QWebInspector(self)
self.webInspector.setPage(page)
shortcut = QShortcut(self)
shortcut.setKey(QKeySequence(Qt.Key_F12))
shortcut.activated.connect(self.toggleInspector)
self.webInspector.setVisible(False)
def toggleInspector(self):
self.webInspector.setVisible(not self.webInspector.isVisible())
if __name__ == '__main__':
def update():
frame.findFirstElement("div").appendInside("TICK ")
app = QApplication(sys.argv)
window = Window()
window.show()
window.view.setHtml(html)
frame = window.view.page().mainFrame()
printer = ConsolePrinter()
frame.addToJavaScriptWindowObject('printer', printer)
timer = QTimer(app)
timer.timeout.connect(update)
timer.start(1000)
app.exec_()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment