Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PySide Editable Label
#!/usr/bin/env python
from PySide import QtCore, QtGui
class KeyPressHandler(QtCore.QObject):
"""Custom key press handler"""
escapePressed = QtCore.Signal(bool)
returnPressed = QtCore.Signal(bool)
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
event_key = event.key()
if event_key == QtCore.Qt.Key_Escape:
self.escapePressed.emit(True)
return True
if event_key == QtCore.Qt.Key_Return or event_key == QtCore.Qt.Key_Enter:
self.returnPressed.emit(True)
return True
return QtCore.QObject.eventFilter(self, obj, event)
class EditableLabelWidget(QtGui.QWidget):
"""Sample Widget"""
def __init__(self, parent=None, **kwargs):
super(EditableLabelWidget, self).__init__(parent)
# create the editable label
self.label = EditableLabel(self)
self.mainLayout = QtGui.QHBoxLayout()
self.mainLayout.addWidget(self.label,)
self.setLayout(self.mainLayout)
self.label.setText("click me to edit")
self.setWindowTitle("Editable Label")
# connect our custom signal
self.label.textChanged.connect(self.labelTextChangedAction)
def labelTextChangedAction(self, text):
print "# label updated: \"{0}\"".format(text)
class EditableLabel(QtGui.QWidget):
"""Editable label"""
textChanged = QtCore.Signal(str)
def __init__(self, parent=None, **kwargs):
QtGui.QWidget.__init__(self, parent=parent)
self.is_editable = kwargs.get("editable", True)
self.keyPressHandler = KeyPressHandler(self)
self.mainLayout = QtGui.QHBoxLayout(self)
self.mainLayout.setContentsMargins(0, 0, 0, 0)
self.mainLayout.setObjectName("mainLayout")
self.label = QtGui.QLabel(self)
self.label.setObjectName("label")
self.mainLayout.addWidget(self.label)
self.lineEdit = QtGui.QLineEdit(self)
self.lineEdit.setObjectName("lineEdit")
self.mainLayout.addWidget(self.lineEdit)
# hide the line edit initially
self.lineEdit.setHidden(True)
# setup signals
self.create_signals()
def create_signals(self):
self.lineEdit.installEventFilter(self.keyPressHandler)
self.label.mousePressEvent = self.labelPressedEvent
# give the lineEdit both a `returnPressed` and `escapedPressed` action
self.keyPressHandler.escapePressed.connect(self.escapePressedAction)
self.keyPressHandler.returnPressed.connect(self.returnPressedAction)
def text(self):
"""Standard QLabel text getter"""
return self.label.text()
def setText(self, text):
"""Standard QLabel text setter"""
self.label.blockSignals(True)
self.label.setText(text)
self.label.blockSignals(False)
def labelPressedEvent(self, event):
"""Set editable if the left mouse button is clicked"""
if event.button() == QtCore.Qt.MouseButton.LeftButton:
self.setLabelEditableAction()
def setLabelEditableAction(self):
"""Action to make the widget editable"""
if not self.is_editable:
return
self.label.setHidden(True)
self.label.blockSignals(True)
self.lineEdit.setHidden(False)
self.lineEdit.setText(self.label.text())
self.lineEdit.blockSignals(False)
self.lineEdit.setFocus(QtCore.Qt.MouseFocusReason)
self.lineEdit.selectAll()
def labelUpdatedAction(self):
"""Indicates the widget text has been updated"""
text_to_update = self.lineEdit.text()
if text_to_update != self.label.text():
self.label.setText(text_to_update)
self.textChanged.emit(text_to_update)
self.label.setHidden(False)
self.lineEdit.setHidden(True)
self.lineEdit.blockSignals(True)
self.label.blockSignals(False)
def returnPressedAction(self):
"""Return/enter event handler"""
self.labelUpdatedAction()
def escapePressedAction(self):
"""Escape event handler"""
self.label.setHidden(False)
self.lineEdit.setHidden(True)
self.lineEdit.blockSignals(True)
self.label.blockSignals(False)
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
widget = EditableLabelWidget()
widget.show()
widget.move(100, 100)
widget.resize(300, 45)
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment