0

I want to get everything I write in the html text input field using the eventFilter method, or is it possible in any other way?

from PyQt5 import QtCore, QtGui, QtWidgets, QtWebEngineWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys 
import os

class MouseTracker(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Mouse Tracker')
        self.web = QtWebEngineWidgets.QWebEngineView(self)
        self.web.installEventFilter(self)
           self.web.load(QtCore.QUrl().fromLocalFile(os.path.split(os.path.abspath(__file__)) 
          [0]+r'\test.html'))
        self.web.resize(300, 80)
        self.show()

    def eventFilter(self, obj, event):
        if event.type() == QtCore.QEvent.KeyPress:
            key = event.text()
            print(key)
        return super().eventFilter(obj, event)


app = QApplication(sys.argv)
ex = MouseTracker()
sys.exit(app.exec_())
  • 1
    I had some doubts with your first question. What you're asking, while technically feasible, represents a *serious* security risk. Why do you want to do this? – musicamante Dec 15 '21 at 21:59
  • 1
    @dabbler it actually doesn't work due to the way QWebEngineView works: input events are actually managed by an internal widget that is what is actually responsible of showing the web contents and handle user input. – musicamante Dec 15 '21 at 22:01
  • This should be done using javascript. Register a [web-channel](https://stackoverflow.com/q/41877799/984421), and then [inject a script into the web-page](https://doc.qt.io/qt-5/qwebenginescript.html) that installs the necessary event-filters on the specific input field(s). The event-handler can then send the results back to the Qt side via the web-channel. – ekhumoro Dec 16 '21 at 00:42
  • Hello everyone, I am a Civil engineering professor and I want to create a software that has the ability to draw shapes and write mathematical formulas at the same time. According to the previous questions I asked you, in Python it is not possible to write mathematical expressions directly, because it is possible to write mathematical expressions with JavaScript. This part of my software has been created. Now I want to control the math input field, for example, by receiving every word I enter in the math field, I can control the dimensions of the math field. – Amanullah Amini Dec 16 '21 at 07:28
  • See you dear friends! You are my last option. I have checked all YouTube and other educational institutions and when I do not get an answer, I ask you and I apologize to you. Maybe you are tired. – Amanullah Amini Dec 16 '21 at 07:34
  • @AmanullahAmini mmh, I see your problem, but it seems a bit awkward to do it like that. What if the user moves the text cursor with the mouse? And what about clipboard management (if the user copies and pastes some text *from* the page or if paste happens from an external source)? How can you you then know what has actually changed? Maybe the user has selected the whole text and typed a single letter, and you wouldn't know. Note that there ways to interact with the webpage and javascript. Try looking look into [QWebChannel](https://doc.qt.io/qt-5/qwebchannel.html) and related questions/answer. – musicamante Dec 17 '21 at 20:11
  • tanks alot my dear teacher – Amanullah Amini Dec 18 '21 at 13:15

0 Answers0