Search code examples
c++qtqabstractitemmodel

What should overloaded QAbstractItemModel::flags return for non valid QModelIndex?


I'm reading a QT documentation for a model/view architecture https://doc.qt.io/qt-5/model-view-programming.html#making-the-model-editable and see an example of overloading QAbstractItemModel::flags method that returns Qt::ItemIsEnabled for invalid index:

Qt::ItemFlags StringListModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;

    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}

So, if index is not valid, i.e. can have negative row, we still consider that user can interact with it. Is there any sence for that logic? For me, returning Qt::NoItemFlags in that case could be more logical


Solution

  • What you need is Qt::NoItemFlags: https://doc.qt.io/Qt-5/qt.html#ItemFlag-enum.

    It's the default value of the flags enum (since it's the first item in the enum). You could also write it as return {};, and it will give you the same Qt::NoItemFlags.