简体   繁体   中英

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, ie 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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM