Getting and Passing the last QKeyEvent raised in PyQt5 through textChanged Connection

42 views Asked by At

Hello I am using PyQt5 QTextEdit() and I want to bind the textChanged() to self._backspaceissue() where self._backspaceissue() is defined as:

@pyqtSlot(QKeyEvent)
def _backspaceissue(self,event):...

I saw this question and thought it may apply but I don't understand any of the functionality behind it so I am not fully sure if this can help: https://stackoverflow.com/questions/45090982/passing-extra-arguments-through-connect This mentions using functools.partial() or lambda but I don't see how that could work. Though before we can do this is there a way to get the QKeyEvent that caused the textChanged?

I have tried looking into the __dir__ of the QTextEdit and found nothing of use and no documentation over it

EDIT: I will add my code for _backspaceissue() and keyPressEvent():

@pyqySlot(QKeyEvent)
def _backspaceissue(self,event):
        try:
            if event.key() == Qt.Key_Backspace:
                if self.toPlainText().split('\n')[-1] == '' or self.toPlainText().split('\n')[-1] in self.disallowed_deletes:pass
                else:super().keyPressEvent(event)
            else:super().keyPressEvent(event)
        except AttributeError:pass
def keyPressEvent(self, e) -> None:
    a=QTextCursor(self.document())
    self.last_event=e
    match e.key():
        case Qt.Key_Backspace:
            #check if line is empty and if so do nothing else remove the last character
            print("Backspace hit: TEXT="+self.toPlainText().split('\n')[-1])
            if self.toPlainText().split('\n')[-1] == '' or self.toPlainText().split('\n')[-1] in self.disallowed_deletes:pass
            else:super().keyPressEvent(e)
        case Qt.Key_Return:
            #take the line of text and push it up one line and print the output in the current line then display the next input
            self.moveCursor(QTextCursor.End)
            print(f"Enter hit: TEXT="+self.toPlainText().split('\n')[-1])
            final_output=self._handleCommand(self.toPlainText().split('\n')[-1].removeprefix(self.prefix))
            self.insertHtml('<br>' + final_output + f'<br>{self.prefix}')
        case _:super().keyPressEvent(e)

I wanted to use something like this:

self.textChanged.connect(partial(self._backspaceIssue, self.last_event))
0

There are 0 answers