Obtain cursor location information in QPlainTextEdit widget

Maurizio Berti maurizio.berti at gmail.com
Tue Mar 23 15:30:47 GMT 2021


Since changing usually happens only when typing *or* pasting, you could
"save" the current contents by overriding keyPressEvent
and insertFromMimeData.
Something like this (the implementation depends on what you actually need
to do afterwards):

class Editor(QtWidgets.QPlainTextEdit):
    def __init__(self):
        super().__init__()

        self.setPlainText('abc')
        self.storePreviousContent()

    def storePreviousContent():
        self.previous_text = self.toPlainText()
        self.previous_position = self.textCursor().position()

    def insertFromMimeData(self, source):
        self.storePreviousContent()
        super().insertFromMimeData(source)

    def keyPressEvent(self, event):
        self.storePreviousContent()
        super().keyPressEvent(event)

Maurizio
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20210323/5c2e7830/attachment.htm>


More information about the PyQt mailing list