0

How do I access the variable x, which is the length of the input characters of an html input field, into the mousePressEvent function? And the variable x is a dynamic variable that increases with the length of each character. I want to control the text input dimensions inside the mousePressEvent. But I can not access the variable x.

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


class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.x = 50
        self.setGeometry(300, 200, 800, 600)
        self.channel = QtWebChannel.QWebChannel(self)
        self.channel.registerObject("backend", self)
        self.show()

    @QtCore.pyqtSlot(str)
    def pyFun1(self, x):
        self.x = x
        print(self.x)
    
    def mousePressEvent(self, e):
        self.view = QtWebEngineWidgets.QWebEngineView(self)
        self.view.resize(self.x, 40)
        self.view.page().setWebChannel(self.channel)
        self.view.load(QtCore.QUrl().fromLocalFile(
            os.path.split(os.path.abspath(__file__)) 
[0]+r'\index.html'))
        self.view.move(QPoint(e.x(), e.y()))
        self.view.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())
  • I would be *very* careful in adding a *new* web view whenever the user presses the mouse, especially considering that the mouse press could be captured by other views, and the fact that each web view could significantly increase memory usage. In any case, just add `self.x = 0` (or whatever default value you prefer) in the `__init__`, and use `self.x = x` in `pyFun1`. But considering the awkward code you posted, that could be a problem: `self.x` will always be updated by ***any*** new web view, so the result could easily become unreliable and *very* confusing to the user. – musicamante Dec 20 '21 at 02:18
  • What __init__ is your goal ??? – Amanullah Amini Dec 20 '21 at 07:14
  • What are you asking? – musicamante Dec 20 '21 at 09:12

0 Answers0