Edit
For capturing manual edits:
class MyLineEdit(QtGui.QLineEdit):
textModified = QtCore.pyqtSignal(str, str) # (before, after)
def __init__(self, contents='', parent=None):
super(MyLineEdit, self).__init__(contents, parent)
self.returnPressed.connect(self.checkText)
self._before = contents
def focusInEvent(self, event):
if event.reason() != QtCore.Qt.PopupFocusReason:
self._before = self.text()
super(MyLineEdit, self).focusInEvent(event)
def focusOutEvent(self, event):
if event.reason() != QtCore.Qt.PopupFocusReason:
self.checkText()
super(MyLineEdit, self).focusOutEvent(event)
def checkText(self):
if self._before != self.text():
self._before = self.text()
self.textModified.emit(self._before, self.text())
Edit 2
For capturing all edits (programmatic and manual):
class MyLineEdit(QtGui.QLineEdit):
textModified = QtCore.pyqtSignal(str, str) # (before, after)
def __init__(self, contents='', parent=None):
super(MyLineEdit, self).__init__(contents, parent)
self.editingFinished.connect(self.checkText)
self.textChanged.connect(lambda: self.checkText())
self.returnPressed.connect(lambda: self.checkText(True))
self._before = contents
def checkText(self, _return=False):
if (not self.hasFocus() or _return) and self._before != self.text():
self._before = self.text()
self.textModified.emit(self._before, self.text())
Edit 3
For capturing only text changes by the user:
class MyLineEdit(QtGui.QLineEdit):
textModified = QtCore.pyqtSignal(str, str) # (before, after)
def __init__(self, contents='', parent=None):
super(MyLineEdit, self).__init__(contents, parent)
self.editingFinished.connect(self.__handleEditingFinished)
self.textChanged.connect(self.__handleTextChanged)
self._before = contents
def __handleTextChanged(self, text):
if not self.hasFocus():
self._before = text
def __handleEditingFinished(self):
before, after = self._before, self.text()
if before != after:
self._before = after
self.textModified.emit(before, after)
event.reason()
). Also, the signal won't fire if return/enter is pressed - so some keyboard handling is needed. – Kelso