Search code examples
c++qttableviewcelledit

QTableView editing mode


When editing a cell of QTableView it opens empty entry. I have seen some tables that inserts the previous text in new opened entry and selects it when editing the cell, but cant find its implementation. Is there standard option for QTableview or it should be achieved manually.


Solution

  • The data of your tree view node while it is in edit mode defined by QAbstractItemModel::data() function with Qt::EditRole as the second argument. The given example makes tree view to show "Editing..." string in editor (usually line edit widget) when your node triggers the edit mode:

    QVariant TreeModel::data(const QModelIndex &index, int role) const
    {
        if (!index.isValid())
            return QVariant();
    
        if (role == Qt::EditRole) {
            return QString("Editing...");
        } else if (role == Qt::DecorationRole) {
            [..]
        } else {
            return QVariant();
        }
    }