接上一篇,在stringlistmodel.h中添加
Qt::ItemFlags flags(const QModelIndex &index) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
在stringlistmodel.cpp中添加:
Qt::ItemFlags MyStringListModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool MyStringListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (index.isValid() && role == DescriptionRole)
{
stringList.replace(index.row(), value.toString());
emit dataChanged(index, index);
return true;
}
return false;
}
这里还是贴出全部代码:
stringlistmodel.h
#ifndef STRINGLISTMODEL_H
#define STRINGLISTMODEL_H
#