Skip to content

Instantly share code, notes, and snippets.

@hahastudio
Created December 20, 2012 13:49
Show Gist options
  • Save hahastudio/4345418 to your computer and use it in GitHub Desktop.
Save hahastudio/4345418 to your computer and use it in GitHub Desktop.
A TextEdit editor that sends editingFinished events when the text was changed and focus is lost.
from PyQt4 import QtCore, QtGui
class TextEdit(QtGui.QTextEdit):
"""
A TextEdit editor that sends editingFinished events
when the text was changed and focus is lost.
"""
editingFinished = QtCore.pyqtSignal()
receivedFocus = QtCore.pyqtSignal()
def __init__(self, parent):
super(TextEdit, self).__init__(parent)
self._changed = False
self.setTabChangesFocus( True )
self.textChanged.connect( self._handle_text_changed )
def focusInEvent(self, event):
super(TextEdit, self).focusInEvent( event )
self.receivedFocus.emit()
def focusOutEvent(self, event):
if self._changed:
self.editingFinished.emit()
super(TextEdit, self).focusOutEvent( event )
def _handle_text_changed(self):
self._changed = True
def setTextChanged(self, state=True):
self._changed = state
def setHtml(self, html):
QtGui.QTextEdit.setHtml(self, html)
self._changed = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment