shao.lo
shao.lo

Reputation: 4626

PyQt monkey patching QLineEdit.paste?

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 either. Anyone know why?

import sys
from PyQt4 import QtGui

def myPaste():
  print("paste") # Never gets here

if __name__ == "__main__":
#    QtGui.QLineEdit.paste = myPaste # Try #1
    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()    
    window.setWindowTitle("monkey")
    centralWidget = QtGui.QWidget(window)

    edit = QtGui.QLineEdit(centralWidget)
#    QtGui.QLineEdit.paste = myPaste # Try #2
    edit.paste = myPaste # Try #3

    window.setCentralWidget(centralWidget)
    window.show()    
    app.exec_()

Based on feedback..i was able to use the event filter suggestion to solve my problem. Updated example code follows...

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = QtGui.QMainWindow()    
    window.setWindowTitle("monkey")

    centralWidget = QtGui.QWidget(window)
    edit = QtGui.QLineEdit(centralWidget)

    window.setCentralWidget(centralWidget)

    def eventFilter(obj, e):
        if isinstance(obj, QtGui.QLineEdit):
            if (e.type() == QtCore.QEvent.KeyPress):
                if (e.matches(QtGui.QKeySequence.Paste)):
                    obj.paste()
                    t=str(obj.text()).title() # Special handling here...uppercase each word for example
                    obj.setText(t)
                    return True
            return False
        else:
            return QtGui.QMainWindow.eventFilter(obj, e)

    window.eventFilter = eventFilter
    edit.installEventFilter(window)

    window.show()    
    app.exec_()

Upvotes: 2

Views: 1071

Answers (2)

ekhumoro
ekhumoro

Reputation: 120638

The reason you can't "monkey-patch" QLineEdit.paste() is because it's not a virtual function. The important thing about virtual functions is that when they are overridden, the reimplemented function will get called internally by Qt; whereas non-virtual overrides will only get called by Python code. So, since QLinedit.paste() isn't virtual, you will instead have to intercept all the events that would normally result in it being called internally by Qt.

That will mean reimplementing QLineEdit.keyPressEvent, so that you can trap the shortcuts for the default key bindings; and also QLineEdit.contextMenuEvent, so that you can modify the default context menu. And, depending on what you're trying to do, you might also need to override the default drag-and-drop handling. (If you prefer not to use a subclass, an event-filter can be used to monitor all the relevant events).

The QClipboard class provides access to the system clipboard, which will allow you to intercept the text before it is pasted. Every application has a single clipboard object, which can be accessed via QApplication.clipboard() or qApp.clipboard().

Upvotes: 5

Vicent
Vicent

Reputation: 5452

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.

Upvotes: 3

Related Questions