qt - How get a focus element in QWebView/QWebPage? -


I should be able to react to focus changes in QWebPage I used the microphone switch () signal and it is almost desirable to me Treats, but anyway I do not know which element to be selected. I want to take some actions when any editable element on the page focus or loses

Thank you in advance

To manage any HTML event in a page, use the following:

  1. To achieve events, create a QObject with the callback slot. This is something dedicated handler object Or your existing QDialog / QMainWindow can be an existing object.
  2. Ask QObject for javascript access with register QWebFrame.addToJavaScriptWindowObject (name, object).
  3. Write JavaScript to HTML Event Handlers, which call the callback slot of your registered cue object.

To add focus handlers, javascript should cross all text elements and add onfocus and onlur handler. Better yet, add a single handler for document alignment and use event bullying. Unfortunately, onblur ads do not have onfocus bubble Fortunately, they have Boomling equivalents called DOMFocusIn and DOMFocusOut.

Here is a complete test for catching focus in / out events for page text elements. It has two slots handleFocusIn and handleFocusOut declared as is called in Java and the main global designated global JavaScript variables to registers on the main window MainWindow < / Code>. To force DOMFocusIn / DOMFocusOut events in these slots, JavaScript runs (indirectly, because we have to filter non-text elemnts).

  import sip sip.setapi ( "QString", 2) sip.setapi ( "QVariant", 2) PyQt4 import QtCore, QtGui, the 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 ( "" "& LT; html & gt; & Lt; body & gt; & lt; input type = "text" name = "text1" /> 
input type = "text" name = "text2" /> Gt; & lt; br / & gt; Input Type = "Submit" value = "OK" /> gt; & lt; / body & gt; & lt; / html & gt; "" ") .setCentralWidget (web_view ) Main_frame = web_view.page () Mukyfrem () main_frame.addToJavaScriptWindowObject ( "MainWindow", self) doc_element = main_frame.d OcumentElement () doc_element.evaluateJavaScript ( "" function isTextElement (el) {return el.tagName == "INPUT" & amp; amp; L. 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) handleFocusIn def (self, ELEMENT_NAME): QtGui.QMessageBox.information (self, "HTML Event", "Focus in:" + ELEMENT_NAME) @ QtCore.pyqtSlot (QtCore.QVariant) def handleFocusOut (self, ELEME NT_NAME): QtGui.QMessageBox.information (auto, "HTML Event", "Out Focus:" + ELEMENT_NAME) application = QtGui.QApplication ([]) Dialog = MyDialog () dialog.show () app.exec_ () < / Code>

(I can rewrite in C ++ if you can not really understand Python).


Comments