Skip to content

Instantly share code, notes, and snippets.

@tao4yu
Last active December 27, 2015 01:59
Show Gist options
  • Save tao4yu/7248648 to your computer and use it in GitHub Desktop.
Save tao4yu/7248648 to your computer and use it in GitHub Desktop.
PySide 本地 QObject 对象与 QtWebKit 中的JavaScript 对象之间的互操作。
# Add external css file to webkit
self.view.settings().setUserStyleSheetUrl(QtCore.QUrl.fromLocalFile("selectElem.css"))
# Add Local QObject and expose its slots and properties to JavaScript Context
self.view.page().currentFrame().addToJavaScriptWindowObject('htmlTextEdit', self.htmlTextEdit)
# Load Local jquery file to webkit
jqueryfile = QtCore.QFile("jquery-1.10.2.min.js");
jqueryfile.open(QtCore.QIODevice.ReadOnly | QtCore.QIODevice.Text);
jQueryJs = jqueryfile.readAll().data();
jqueryfile.close();
# webkit execute external js file
self.view.page().currentFrame().evaluateJavaScript(jQueryJs)
code = "var flag2 = null;\
$().ready(function(){\
$('*').click(function(){\
if(flag2 != null){\
flag2.removeClass('elemselected');\
}\
flag2 = $(this);\
htmlTextEdit.setPlainText($(this).html());\
$(this).addClass('elemselected');\
return false;\
});\
}); null;"
# webkit execute js code
self.view.page().mainFrame().evaluateJavaScript(code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment