PyQt monkey patching QLineEdit.paste?

前端 未结 2 1328
北海茫月
北海茫月 2021-01-16 07:07

I am trying to intercept paste() for a specific edit box. After much reading and head scratching I decided to try the big hammer and monkey patch. This didn\'t work for me

2条回答
  •  失恋的感觉
    2021-01-16 07:27

    In order to do what you want you can subclass QLineEdit and create a method that provides the custom paste functionality that you want (paste method isn't virtual so if it is overriden it won't be called from Qt code). In addition you will need an event filter to intercept the shortcut for CTRL+V. Probably you will have to filter the middle mouse button too which is also used to paste the clipboard content. From the event filter you can call your replacement of paste method.

    You can use the following code as starting point:

    import sys
    from PyQt4.QtGui import *
    from PyQt4.QtCore import *
    
    class myEditor(QLineEdit):
        def __init__(self, parent=None):
            super(myEditor, self).__init__(parent)
    
        def myPaste(self):
            self.insert("custom text pasted! ")
    
    class myWindow(QMainWindow):
        def __init__(self, parent=None):
            super(myWindow, self).__init__(parent)
            self.customEditor = myEditor(self)
            self.setCentralWidget(self.customEditor)
            self.customEditor.installEventFilter(self)
    
        def eventFilter(self, obj, e):
            if (obj == self.customEditor):
                if (e.type() == QEvent.KeyPress):
                    if (e.matches(QKeySequence.Paste)):
                        self.customEditor.myPaste()
                        return True
                return False
            else:
                return QMainWindow.eventFilter(obj, e)
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = myWindow()
        window.show()    
        app.exec_()
    

    The event filter here only takes care of the keyboard shortcut for pasting. As I said you need to consider also other sources of the paste operation.

提交回复
热议问题