QT6 源(126)QModelIndex 模型索引:阅读注释与测试其成员函数,及其源代码。以及 typedef QList<QModelIndex> QModelIndexList;

(1)模型里条目的索引是支持调试打印的

在这里插入图片描述

(2)还结合上例的测试结果,给出本模型索引的定义,如下

在这里插入图片描述

(3)继续本模型索引类的成员函数的测试,还使用上面的例子提供的实验程序

在这里插入图片描述

(4)构造函数,本类型的空值表示本索引不指向 model 中的任何条目

在这里插入图片描述

(5)测试仍使用同一例子

在这里插入图片描述

(6)感觉这俩函数没啥用

在这里插入图片描述

(7)

在这里插入图片描述

(8)索引之间的大小比较

在这里插入图片描述

++补充

在这里插入图片描述

++给出举例

在这里插入图片描述

(9)获取索引指向的模型条目里的对应某角色的数据

在这里插入图片描述

++ 一次性获得多个角色的数据

在这里插入图片描述

(10)还是使用前面的例子做实验测试模型里条目的属性

在这里插入图片描述

(11)返回本索引指向的条目的兄弟姐妹条目的索引

在这里插入图片描述

++ 以及

在这里插入图片描述

++举例测试

在这里插入图片描述

(12)给出本类的源代码,定义于头文件 qabstractitemmodel . h

/*
The QModelIndex class is used to locate data in a data model.

该类用作对源自 QAbstractltemModel 的项目模型的索引。
索引由项目视图、委托和选择模型使用,以在模型中定位项目。
This class is used as an index into item models derived from QAbstractItemModel.
The index is used by item views, delegates,
and selection models to locate an item in the model.

新的 QModellndex对象由模型使用QAbstractltemModel::createlndex()函数创建。
可以使用QModelIndex构造函数构建无效的模型索引。
无效索引通常用作父索引,在引用模型中的顶级项时。
New QModelIndex objects are created by the model using the
    QAbstractItemModel::createIndex() function.
An invalid model index can be constructed with the QModelIndex constructor.
Invalid indexes are often used as parent indexes
when referring to top-level items in a model.

模型索引引用模型中的项目,并包含指定它们在这些模型中的位置所需的所有信息。
每个索引都位于给获取此信息。模型中定的行和列中,并且可能具有父索引.
使用row()、column()和parent()来获取这些信息。
每个顶级项目都由一个没有父索引的模型索引表示-在这种情况下,parent()将返回一个无效的模型索引,
相当于使用 QModellndex()构造函数的零参数形式构造的索引。
Model indexes refer to items in models,
and contain all the information required to specify their locations in those models.
Each index is located in a given row and column, and may have a parent index;
use row(), column(), and parent() to obtain this information.
Each top-level item in a model is represented by a model index
    that does not have a parent index
    - in this case, parent() will return an invalid model index,
    equivalent to an index constructed with the zero argument form of the
    QModelIndex() constructor.

要获取引用模型中现有项目的模型索引,
请使用所需的行和列值以及父项的模型索引,来调用QAbstractltemModel::index()。
当引用模型中的顶级项目时,请提供QModellndex()作为父索引。
To obtain a model index that refers to an existing item in a model,
call QAbstractItemModel::index() with the required row and column values,
and the model index of the parent.
When referring to top-level items in a model, supply QModelIndex() as the parent index.

model()函数返回索引引用为 QAbstractltemModel的模型。
child()函数用于检查模型中索引下保存的项目。
sibling()函数允许您在与索引相同的级别上遍历模型中的项目。
The model() function returns the model that the index references as a QAbstractItemModel.
The child() function is used to examine items held under the index in the model.
The sibling() function allows you to
    traverse items in the model on the same level as the index.

注:模型索引应即时使用并随后丢弃。
不应依赖索引,因为在调用会改变模型结构或删除项的模型函数后,索引可能不再有效。
如果您需要在较长时间内保留模型索引,应使用QPersistentModellndex。
Note: Model indexes should be used immediately and then discarded.
You should not rely on indexes to remain valid after calling model functions
    that change the structure of the model or delete items.
If you need to keep a model index over time use a QPersistentModelIndex.

*/

class QModelIndex       //模型索引
{
    friend class QAbstractItemModel; //友元类

private:
    int       r, c; // item条目的行号、列号与
    quintptr  i   ; //typedef QIntegerForSizeof<void *>::Unsigned quintptr;
                    //系统又给本索引指向的条目 item 定义了一个 id,
                    //但测试指出本数据成员 i 不是本索引指向的条目的内存地址,因为会重复
                    //本值似乎仍然是贡献于树形 model 的条目定位,且不一定永远为 0。随后再测试。
    const   QAbstractItemModel * m; //本索引指向的条目的所在模型的内存地址

    //因为构造函数被定义为私有的。外界就不能直接生成 QModelIndex索引对象,
    //只能由其友元模型类来给出对应条目的索引。
    constexpr inline
    QModelIndex(int arow        , int acolumn,  //私有的有参构造函数
                quintptr      id, const QAbstractItemModel * amodel) noexcept
        : r(arow), c(acolumn), i(id)                             , m(amodel) {}

    inline
    QModelIndex(int arow        , int acolumn,  //私有的有参构造函数,形参 3是对象指针
                const void * ptr, const QAbstractItemModel * amodel) noexcept
        : r(arow), c(acolumn), i(reinterpret_cast<quintptr>(ptr)), m(amodel) {}

public:
    //Creates a new empty model index.
    //This type of model index is used to indicate that the
    //                                  position in the model is invalid.
    constexpr inline QModelIndex() noexcept : r(-1), c(-1), i(0), m(nullptr) {}//默认构造函数

    // compiler-generated copy/move ctors/assignment operators are fine!
    //意思是 copy构造函数、移动构造函数,赋值运算符函数依然是有效的。

    //Returns the row / column this model index refers to.
    constexpr inline       int                 row       () const noexcept { return r; }
    constexpr inline       int                 column    () const noexcept { return c; }
    constexpr inline       quintptr            internalId() const noexcept { return i; }
    //Returns a quintptr used by the model to associate the
    //  index with the internal data structure.

    //返回指向包含此索引所指项目的模型的指针。返回本索引指向的条目所在的模型的地址。
    //Returns a pointer to the model containing the item that this index refers to.
    //返回的是该模型的常引用指针,因为调用模型的非常量函数可能会使模型索引失效,并可能导致应用程序崩溃。
    constexpr inline const QAbstractItemModel * model    () const noexcept { return m; }

    //Returns the parent of the model index, or QModelIndex() if it has no parent.
    inline  QModelIndex  parent() const
    { return m ? m->parent(*this) : QModelIndex(); }
    //QModelIndex QAbstractItemModel::parent(const QModelIndex & child)


    //Returns a       void * pointer used by the model to associate the
    //  index with the internal data structure.
    inline       void *      internalPointer() const noexcept
    { return reinterpret_cast<      void *>(i); }

    inline const void * constInternalPointer() const noexcept
    { return reinterpret_cast<const void *>(i); }
    //Returns a const void * pointer used by the model to associate the
    //  index with the internal data structure.

    //Returns true if this model index is valid; otherwise returns false.
    //A valid index belongs to a model, and has non-negative row and column numbers.
    //有效的模型索引值是指向某个存在的模型且索引指向的条目的行列值不为空
    constexpr inline bool isValid() const noexcept
    { return (r >= 0) && (c >= 0) && (m != nullptr); }

    constexpr inline bool operator==(const QModelIndex & other) const noexcept
    { return (other.r == r) && (other.c == c) && (other.i == i) && (other.m == m); }

    constexpr inline bool operator!=(const QModelIndex & other) const noexcept
    { return !(*this == other); }

    //总之是依次比较数据成员 行r、 列c、内部id、所在模型的地址m 的取值,有小于 <成立的即为真。
    constexpr inline bool operator< (const QModelIndex & other) const noexcept
    {
        return  r <  other.r
                ||  ( r == other.r
                        &&  ( c <  other.c
                                || ( c == other.c
                                    && (i <  other.i
                                        || (i == other.i
                     && std::less<const QAbstractItemModel *>()(m, other.m)
                                            )
                                        )
                                    )
                            )
                    );
    }

    //Returns the data for the given role for the item referred to by the index.
    //返回本索引指向的模型中条目里的某种角色的数据
    inline QVariant data(int role = Qt::DisplayRole) const
    { return m ? m->data(* this, role) : QVariant(); }
    //QVariant QAbstractItemModel::data(QModelIndex & index, int role = Qt::DisplayRole)

    //class QModelRoleDataSpan { QModelRoleData * m_modelRoleData = nullptr;
    //                           qsizetype        m_len           = 0      ;  };
    //Populates the given roleDataSpan for the item referred to by the index.
    //一次性从本索引指向的 item 里获取多个角色的数据。
    inline void multiData(QModelRoleDataSpan roleDataSpan) const //Qt6 里才有本函数
    {   
        if (m)
            m->multiData(*this, roleDataSpan);
    }
//void QAbstractItemModel::multiData(QModelIndex & index, QModelRoleDataSpan roleDataSpan);
    
    /*
    enum Qt::ItemFlag {
        NoItemFlags = 0,
        ItemIsSelectable = 1,
        ItemIsEditable = 2,
        ItemIsDragEnabled = 4,
        ItemIsDropEnabled = 8,
        ItemIsUserCheckable = 16,
        ItemIsEnabled = 32,
        ItemIsAutoTristate = 64,
        ItemNeverHasChildren = 128,
        ItemIsUserTristate = 256
    };
    Q_DECLARE_FLAGS(ItemFlags, ItemFlag) 
    */  //返回本索引指向的条目具有的属性。
    inline Qt::ItemFlags flags() const 
    { return m ? m->flags(*this) : Qt::ItemFlags(); }
    //Returns the flags for the item referred to by the index.
    //Qt::ItemFlags QAbstractItemModel::flags(const QModelIndex & index) const;
    
    //Returns the sibling at row and column.
    // If there is no sibling at this position, an invalid QModelIndex is returned.
    inline QModelIndex sibling(int row, int column) const
    {
        return  m
                ? (r == arow && c == acolumn)
                    ? * this
                    : m->sibling(arow, acolumn, *this)
                : QModelIndex();
    //QModelIndex QAbstractItemModel::sibling(int row, int column, QModelIndex & idx);
    }
    
    //Returns the sibling at column for the current row.
    //If there is no sibling at this position, an invalid QModelIndex is returned.
    inline QModelIndex siblingAtColumn(int column) const
    {
        return  m
                ? (c == acolumn)
                    ? * this
                    : m->sibling(r, acolumn, *this)
                : QModelIndex();
    }

    //Returns the sibling at row for the current column. 
    inline QModelIndex siblingAtRow(int row) const //sibling兄弟姐妹
    {
        return
                m
                ? (r == arow)
                    ? *this
                    : m->sibling(arow, c, *this)
                : QModelIndex();
    }

}; //完结 class QModelIndex
Q_DECLARE_TYPEINFO(QModelIndex, Q_RELOCATABLE_TYPE); //给出了本类的存储类型

//说明模型索引支持调试输出
Q_CORE_EXPORT QDebug operator<<(QDebug, const QModelIndex &);


//本全局似乎是为了生成对应本索引的哈希值,并结合形参的种子。
inline size_t qHash(const QModelIndex & index, size_t seed = 0) noexcept
{
    return  size_t(  ( size_t(index.row()) << 4 )
                    + size_t(index.column())
                    + index.internalId()
                    ) ^ seed;
}

(13)

谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangzhangkeji

谢谢支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值