如何在QWebView/QWebPage中获得焦点元素? [英] How get a focus element in QWebView/QWebPage?

查看:135
本文介绍了如何在QWebView/QWebPage中获得焦点元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够对QWebPage中的焦点更改做出反应.我使用了microFocusChanged()信号,它给了我几乎理想的行为,但是无论如何我都不知道如何知道选择了哪个元素.当页面上的任何可编辑元素获得焦点或失去焦点时,我想执行一些操作.

i need to be able to react on focus changes in QWebPage. I used microFocusChanged() signal and it gives me almost desirable behavior, but anyway i don't know how to know which element is selected. I want to do some actions when any editable element on page gets or loses focus.

提前谢谢

推荐答案

要处理页面中的任何HTML事件,请执行以下操作:

To handle any HTML event within a page you do the following:

  1. 创建带有回调插槽的QObject来接收事件.它可能是一些专用的处理程序对象,也可能是诸如QDialog/QMainWindow之类的现有对象.
  2. 使用QWebFrame.addToJavaScriptWindowObject(名称,对象)注册用于JavaScript访问的QObject.
  3. 编写JavaScript以添加HTML事件处理程序,以调用已注册的QObject的回调槽.

添加焦点更改处理程序的JavaScript应该遍历所有文本元素,并添加onfocus和onblur处理程序.更好的方法是将单个处理程序添加到documentElement并使用事件冒泡.不幸的是,onblur广告的onfocus不会冒泡.幸运的是,它们有一个冒泡的对应对象,称为DOMFocusIn和DOMFocusOut.

JavaScript to add focus change handlers should traverse all text elements and add onfocus and onblur handlers. Better yet is to add single handler to documentElement and use event bubbling. Unfortunately, onblur ad onfocus do not bubble. Fortunately, they have a bubbling counterparts called DOMFocusIn and DOMFocusOut.

这是捕获页面文本元素的焦点入/出事件的完整示例.它在要从JavaScript调用的主窗口上声明两个插槽handleFocusInhandleFocusOut,并将主窗口注册为名为MainWindow的全局JavaScript变量.然后运行JavaScript,将DOMFocusIn/DOMFocusOut事件绑定到这些插槽(间接地,因为我们需要过滤掉非文本元素).

Here's a complete examlpe of catching focus in/out events for page text elements. It declares two slots handleFocusIn and handleFocusOut on the main window to be called from JavaScripts and registers main window as a global JavaScript variable named MainWindow. In then runs JavaScript to bind DOMFocusIn/DOMFocusOut events to these slots (indirectly, because we need to filter out non-text elemnts).

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_()

(如果您真的不懂Python,我可能会用C ++重写).

(I may rewrite in in C++ if you REALLY can't figure out Python).

这篇关于如何在QWebView/QWebPage中获得焦点元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆