ser*_*rge 2 qt qwebview qwebpage
我需要能够对QWebPage中的焦点更改做出反应.我使用了microFocusChanged()信号,它给了我几乎所需的行为,但无论如何我不知道如何知道选择了哪个元素.我希望在页面上的任何可编辑元素获得或失去焦点时执行某些操作.
先感谢您
要处理页面中的任何HTML事件,请执行以下操作:
添加焦点更改处理程序的JavaScript应该遍历所有文本元素并添加onfocus和onblur处理程序.更好的是将单个处理程序添加到documentElement并使用事件冒泡.不幸的是,onblur广告onfocus不会冒泡.幸运的是,他们有一个名为DOMFocusIn和DOMFocusOut的冒泡对手.
这是一个完整的检查页面文本元素的焦点进/出事件.它声明了两个插槽,handleFocusIn并handleFocusOut在主窗口上从JavaScripts调用,并将主窗口注册为名为的全局JavaScript变量MainWindow.然后运行JavaScript将DOMFocusIn/DOMFocusOut事件绑定到这些插槽(间接,因为我们需要过滤掉非文本元素).
import sip
sip.setapi("QString", 2)
sip.setapi("QVariant", 2)
from PyQt4 import QtCore, QtGui, QtWebKit
class MyDialog(QtGui.QMainWindow):
def __init__(self):
super(MyDialog, self).__init__()
self.setWindowTitle("QWebView JavaScript Events")
web_view = QtWebKit.QWebView(self)
web_view.setHtml("""
<html>
<body>
<input type="text" name="text1"/><br/>
<input type="text" name="text2"/><br/>
<input type="submit" value="OK"/>
</body>
</html>""")
self.setCentralWidget(web_view)
main_frame = web_view.page().mainFrame()
main_frame.addToJavaScriptWindowObject("MainWindow", self)
doc_element = main_frame.documentElement()
doc_element.evaluateJavaScript("""
function isTextElement(el) {
return el.tagName == "INPUT" && el.type == "text";
}
this.addEventListener("DOMFocusIn", function(e) {
if (isTextElement(e.target)) {
MainWindow.handleFocusIn(e.target.name);
}
}, false);
this.addEventListener("DOMFocusOut", function(e) {
if (isTextElement(e.target)) {
MainWindow.handleFocusOut(e.target.name);
}
}, false)
""")
self.resize(300, 150)
@QtCore.pyqtSlot(QtCore.QVariant)
def handleFocusIn(self, element_name):
QtGui.QMessageBox.information(
self, "HTML Event", "Focus In: " + element_name)
@QtCore.pyqtSlot(QtCore.QVariant)
def handleFocusOut(self, element_name):
QtGui.QMessageBox.information(
self, "HTML Event", "Focus Out: " + element_name)
app = QtGui.QApplication([])
dialog = MyDialog()
dialog.show()
app.exec_()
Run Code Online (Sandbox Code Playgroud)
(如果你真的无法弄清楚Python,我可能会用C++重写).