Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8960201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:37:07+00:00 2026-06-15T15:37:07+00:00

I am trying to intercept paste() for a specific edit box. After much reading

  • 0

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_()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:37:08+00:00Added an answer on June 15, 2026 at 3:37 pm

    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().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Scenario: I'm trying to intercept paste events inside a textarea/input text, and filter the
I'm trying to intercept WindowsAuthenticationModule to login into application as another user, but I
I'm trying to intercept a method call (afterInsert() of a domain class) in a
I'm trying to intercept events using Gdk.Window.AddFilter(Gdk.FilterFunc) in Mono. So far, I have been
i'm trying to intercept ajax requests with jquery, to display a waiting message like
I am trying to intercept form submits from webpages I dont control. My current
I'm trying to programmatically intercept and cancel a user's attempt to check out a
What I'm trying to achieve: Intercept requests for .asp files using an asp.net application
I'm trying to use jQuery to intercept control-A keypresses on my web page, like
I'm trying to make a small program that could intercept the open process of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.