PySide+Javascript+QWebView連携
なかなか思い通りに動いてくれなくて苦労した
# -*- coding: utf-8 -*- ''' QWebViewの動作確認 ''' from PySide import QtWebKit,QtGui,QtCore import sys HTML = u"""<html> <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> <script> if(!pyObj.say) { document.write("addToJavaScriptWindowObjectがよびだせませんでした"); }else{ document.write(pyObj.say); } pyObj.setMessage("メソッドの呼び出しに成功しました"); </script> <body> <h1>Test Page</h1> <a href="http://google.co.jp">google</a> </body> </html> """ class PyObject(QtCore.QObject): name = "" @QtCore.Slot(str) def setMessage(self, msg): """ javascriptから受け取り """ print msg def _say(self): """ javascriptへデータを送る """ return u"メッセージの呼び出しに成功しました" say = QtCore.Property(str, fget=_say) class MyWebview(QtWebKit.QWebView): def __init__(self, *args, **kwargs): QtWebKit.QWebView.__init__(self, *args, **kwargs) #link ClickedはsetLinkDelegationPolicyを設定しないと有効にならない self.page().setLinkDelegationPolicy(QtWebKit.QWebPage.DelegateExternalLinks) self.connect(self.page(), QtCore.SIGNAL('linkClicked(const QUrl &)'), self.linkClickAction) #javascriptの呼び出し self.connect(self.page().mainFrame(), QtCore.SIGNAL("javaScriptWindowObjectCleared ()"), self.javaScriptWindowObjectCleared) def javaScriptWindowObjectCleared(self): """ javascript連携 """ self.js = PyObject() self.page().mainFrame().addToJavaScriptWindowObject("PyObj", self.js) self.page().mainFrame().evaluateJavaScript (" var MyParameters=1;") def linkClickAction(self, url): print "link clicked!" self.load(url) def mouseReleaseEvent(self, *args, **kwargs): print "Mouse Release!" return QtWebKit.QWebView.mouseReleaseEvent(self, *args, **kwargs) def mouseMoveEvent(self, ev): return QtWebKit.QWebView.mouseMoveEvent(self, ev) """ ev = QMouseEvent """ print "mouseMove" print ev.x() print ev.y() print ev.globalX() print ev.globalY() pass def linkClicked(url): print "link clicked" print url #QtWebKit.QWebView.setUrl(url) def linkHoverd(url): print "link clicked" print url def urlChanged(url): print "url changed" def loadStarted(): print "load start" def selectionChanged(): print "selection changed" def titleChanged(title): print title def statusBarMessage (text): print "status bar message" print text def loadFinished(result): print "finished" print result def loadProgress(prog): print "load progress" print prog def linkHovered(a, b, c): print "link hovered!" print a print b print c app = QtGui.QApplication(sys.argv) view = MyWebview() QtCore.QObject.connect(view, QtCore.SIGNAL('loadStarted ()'), loadStarted) QtCore.QObject.connect(view, QtCore.SIGNAL('loadFinished(bool)'), loadFinished) QtCore.QObject.connect(view, QtCore.SIGNAL('loadProgress(int)'), loadProgress) QtCore.QObject.connect(view.page(), QtCore.SIGNAL('linkHovered(const QString&,const QString&,const QString&)'), linkHovered) QtCore.QObject.connect(view, QtCore.SIGNAL('urlChanged(const QUrl &)'), urlChanged) QtCore.QObject.connect(view, QtCore.SIGNAL('selectionChanged()'), selectionChanged) QtCore.QObject.connect(view, QtCore.SIGNAL('titleChanged(const QString & )'), titleChanged) QtCore.QObject.connect(view.page(), QtCore.SIGNAL('statusBarMessage(const QString&)'), statusBarMessage) view.setHtml(HTML) view.show() # Enter Qt main loop sys.exit(app.exec_())