简体   繁体   English

对于无效的 QModelIndex,重载的 QAbstractItemModel::flags 应该返回什么?

[英]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 文档https://doc.qt.io/qt-5/model-view-programming.html#making-the-model-editable并查看重载 QAbstractItemModel 的示例: :flags 方法为无效索引返回Qt::ItemIsEnabled

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对我来说,在这种情况下返回Qt::NoItemFlags可能更合乎逻辑

What you need is Qt::NoItemFlags : https://doc.qt.io/Qt-5/qt.html#ItemFlag-enum .你需要的是Qt::NoItemFlagshttps Qt::NoItemFlags

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 {};你也可以把它写成return {}; , and it will give you the same Qt::NoItemFlags . ,它会给你相同的Qt::NoItemFlags

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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