Search code examples
c++qtqt4qtreeview

How should I respond to text being changed in a QTreeView?


I have a QTreeView populated with some QStandardItems. The items are editable, and I want to program to react whenever the text of an item is changed, either by way of a signal or an event filter. I can't find anything that fills the bill. I tried QStandardModel::itemChanged, but that responds to changes in other item data, and I don't see a way to distinguish between changes in the text content and user data. What is the best way to approach this?


Solution

  • To deal with this I wound up deriving a class from QStandardItem and overriding QStandardItem::setData something like this:

    void MyStandardItem::setData(const QVariant& value, int role)
    {
        if (role == Qt::EditRole) {
            doStuff();
        }
    
        QStandardItem::setData(value, role);
    }