Skip to content Skip to sidebar Skip to footer

Pyqt Qlineedit And 'paste' Event?

I searched everywhere but couldn't find an example on triggering a slot/event when text gets pasted in an PyQt4 QLineEdit ?

Solution 1:

Add the following code to your MyForm class:

Inside __init__()

self.ui.lineEdit_URL.textChanged.connect(self.valueChanged)

Define new method:

defvalueChanged(self, text):
     if QtGui.QApplication.clipboard().text() == text:self.pasteEvent(text)

Define another method:

defpasteEvent(self, text):
    # this is your paste event, whenever any text is pasted in the# 'lineEdit_URL', this method gets called.# 'text' is the newly pasted text.print text

Solution 2:

You will have to define it yourself by overriding "keyPressEvent". For Example:

from PyQt4 import QtGui, QtCore
import sys

class NoteText(QtGui.QLineEdit):
    def __init__(self, parent):
        super (NoteText, self).__init__(parent)

    def keyPressEvent(self, event):
        if event.matches(QtGui.QKeySequence.Paste):
            self.setText("Bye")

class Test(QtGui.QWidget):
  def __init__( self, parent=None):
      super(Test, self).__init__(parent)

      le = QtGui.QLineEdit()
      nt = NoteText(le)

      layout = QtGui.QHBoxLayout()
      layout.addWidget(nt)
      self.setLayout(layout)

app = QtGui.QApplication(sys.argv)
myWidget = Test()
myWidget.show()
app.exec_()

Post a Comment for "Pyqt Qlineedit And 'paste' Event?"