Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTreeWidgetItem Text Edited Signal



  • Hello,
    This is PyQt4, but is similar to C++.
    After doing a lot of research, it turns out that something as simple as getting a signal when editing the text on a QTreeWidgetItem is annoyingly difficult. There's the QTreeWidget.ItemChanged signal, but it fires for every little change that happens when all I wanted was to get a signal on text change. I was able to figure something out, but it seems kind of hacky. I was wondering if anyone here could share a more elegant way of solving this problem.

    @
    class SignalObject(QObject):
    Edited = pyqtSignal(QTreeWidgetItem)

    def __init__(self):
        super(SignalObject, self).__init__()
    

    class CustomTreeWidgetItem(QTreeWidgetItem):
    def init(self, parent=None):
    super(CustomTreeWidgetItem, self).init(parent)
    self.signalObject = SignalObject()

    def setData(self, column, role, value):
        super(FileTreeWidgetItem, self).setData(column, role, value)
    
        if role == Qt.EditRole:
            self.signalObject.Edited.emit(self)
    

    @

    First problem with this is QTreeWidgetItem doesn't inherit from QObject, so I had to create a custom class that inherits from QObject that stores all of my custom signals. Second problem is, I'm barely changing QTreeWidgetItem's functionality with my inherited class. I have to write all this code just to check if the role was Qt.EditRole.


  • Qt Champions 2017

    Instead of using the QTreeWidget, you can try with QTreeView, SelectioModel and Delegate of QTreeView. Getting the existing delegate and observing some of the singnals from delegate may help you.



  • Thanks, I'll look into this.


Log in to reply