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 theQAbstractItemModel::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 indexthat 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 theQModelIndex() 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 totraverse 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 functionsthat 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 inlineQModelIndex(int arow        , int acolumn,  //私有的有参构造函数quintptr      id, const QAbstractItemModel * amodel) noexcept: r(arow), c(acolumn), i(id)                             , m(amodel) {}inlineQModelIndex(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兄弟姐妹{returnm? (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)

谢谢

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.tpcf.cn/web/86208.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

分布式环境下 Spring Boot 项目基于雪花算法的唯一 ID 生成方案

一、分布式系统 分布式系统是指将多个独立的计算节点通过网络连接&#xff0c;协同完成同一目标的系统架构。其核心特征是&#xff1a; 多个独立节点&#xff1a;每个节点都是一个可独立运行的服务实例网络通信&#xff1a;节点间通过网络协议&#xff08;如HTTP、RPC&#x…

如何在 Chrome 浏览器中保存从商店下载的扩展程序到本地

如何在 Chrome 浏览器中保存从商店下载的扩展程序到本地 方法一&#xff1a;通过扩展程序页面直接导出&#xff08;适用于已安装的扩展&#xff09; 打开 Chrome 扩展管理页面 在地址栏输入&#xff1a;chrome://extensions/或通过菜单&#xff1a;⋮ > 更多工具 > 扩展…

没有VISA怎么注册AWS?

没有VISA怎么注册AWS&#xff1f;跨境卖家、开发者与学生党必看的“AWS账号开通终极指南”&#xff01; 在云计算日益普及的今天&#xff0c;Amazon Web Services&#xff08;AWS&#xff09;作为全球领先的云服务提供商&#xff0c;以其服务广度、技术深度和生态系统成熟度&a…

华为服务器的选型指南

以下是华为服务器选型指南及推荐方案&#xff0c;综合性能、场景适配与成本优化&#xff1a; 一、核心选型维度 业务场景 通用计算&#xff08;Web/中间件&#xff09;&#xff1a;推荐通用型鲲鹏服务器&#xff08;如KH系列&#xff09;&#xff0c;支持多核并发&#xff08…

Python-3-数据结构(字典)

1 字典 特点 1.键-值成对出现 “键&#xff1a;值 ” 2.键不能重复 3.键不可更改&#xff0c;值可以修改 4.键来索引值 5.键只能是不可变的数据类型 dic_Python{the define:1,the age:2,the clude:[A,B] } #增删查改 dic_Python[the age] dic_Python[the define]77#赋值修改 di…

API访问Token的概念及解析

API 访问Token&#xff08;API Access Token&#xff09; 如大家所了解的&#xff0c;API访问Token是用于第三方应用调用服务的关键措施&#xff0c;如支付接口、地图 API等。 许多机构和安全指南&#xff08;例如 OWASP API Security Project&#xff09;建议采用短期 Token…

​​​​​​​[AI 工具] Dify 免费 GPT 调用详解:额度、付费与自托管方案全解

引言:Dify 是什么? Dify 是一个支持多种大模型(如 GPT-3.5、GPT-4、GPT-4o)的开源 AI 应用开发平台,支持 Web UI 快速搭建、多模态能力、团队协作等。其平台免费开放使用 GPT 模型,受到开发者和技术社区广泛关注。 我是Dify重度用户,大多数情况用本地部署,但是忽然发…

构建你的API防护盾 - 抵御恶意爬虫、注入与业务欺诈

现代App和Web应用的核心是API&#xff0c;它也是攻击者的首要目标。恶意爬虫窃取数据、SQL注入篡改数据库、精心构造的请求进行薅羊毛或欺诈… 这些业务逻辑层的攻击&#xff0c;往往能绕过传统防火墙。本文将分享几种实用的API防护技术&#xff0c;并提供可直接部署的代码示例…

从 “人工巡检” 到 “远程智控”,工业路由器实现变电站远程监控

能源电力行业加速数字化转型&#xff0c;负责电力输送与分配的变电站智能化升级迫在眉睫。工业路由器在变电站远程监控领域成功应用&#xff0c;是能源电力物联网建设必不可少的核心通讯设备。 变电站远程监控项目背景 传统变电站监控依赖人工巡检与有线通信&#xff0c;效率低…

xss利用meta强制跳转 CPS report-uri 报错泄露利用 -- GPN CTF 2025 Free Parking Network 1 2

part 1 在此题目中,我们可以指定html与标头 <sCrIpt>alert(1)</ScRipt>A5rz: A5rz服务器会返回如下内容 HTTP/1.1 200 OK X-Powered-By: Express A5rz: A5rz Content-Type: text/html; charsetutf-8 Content-Length: 619 ETag: W/"26b-14GnlOyaaXJ3CEkd0rBJ/m…

1 Web vue环境搭建

1 下载好node.js 用node -v和npm -v看是否环境配置好&#xff0c;看到如下结果就是配置好了 2 安装vue脚手架 输入这个代码 npm i vue/cli -g 查看到如下&#xff0c;说明安装成功 3 下载vue初始模板 输入 vue ui 会打开一个网页 点击创建&#xff0c;然后点击编辑路径&…

太理IM即时通讯软件开发

easyQQ ♻️项目基本介绍 easyQQ是基于electron(vue2)和nodejs实现的简单聊天软件,其中用websocket和http进行通讯传递,数据库使用了mysql数据库,该项目功能简单,界面简洁,每个功能都会添加相应的逻辑 &#x1f9e7; 作者自己的配置环境 数据库 nodejs npm &#x1f9e8; 部…

BERT 模型准备与转换详细操作流程

在尝试复现极客专栏《PyTorch 深度学习实战|24 | 文本分类&#xff1a;如何使用BERT构建文本分类模型&#xff1f;》时候&#xff0c;构建模型这一步骤专栏老师一笔带过&#xff0c;对于新手有些不友好&#xff0c;经过一阵摸索&#xff0c;终于调通了&#xff0c;现在总结一下…

doris 和StarRocks 导入导出数据配置

一、StarRocks 导数据到hdfs EXPORT TABLE database.table TO “hdfs://namenode/tmp/demo/table” WITH BROKER ( “username”“username”, “password”“password” ); 二、StarRocks 导数据到oss EXPORT TABLE database.table TO “oss://broke/aa/” WITH BROKER ( “…

【HTTP】取消已发送的请求

场景 在页面中&#xff0c;可能会因为某些操作多次触发某个请求&#xff0c;如多次点击某按钮触发请求&#xff0c;实际上我们只需要最后一次请求的返回值&#xff0c;但是由于请求的耗时不一&#xff0c;请求未必会按发送的顺序返回&#xff0c;导致我们最终获取到的值 ≠ 最后…

JSON框架转化isSuccess()为sucess字段

在您的描述中&#xff0c;BankInfoVO子类返回的JSON中出现了"success": true字段&#xff0c;但类本身没有定义这个字段。这通常是由以下原因之一造成的&#xff1a; 原因分析及解决方案 序列化框架的Getter自动推导 Java序列化框架&#xff08;如Jackson/Gson&…

Ragflow 源码:task_executor.py

目录 介绍主要功能核心组件 流程图核心代码解释1. 系统架构与核心组件2. 核心处理流程3. 高级处理能力4. 关键创新点5. 容错与监控机制6. 性能优化技巧 介绍 task_executor.py 是RAGFlow系统中的任务执行器(Task Executor)核心部分&#xff0c;主要负责文档的解析、分块(chunk…

创客匠人联盟生态:重构家庭教育知识变现的底层逻辑

在《家庭教育促进法》推动行业刚需化的背景下&#xff0c;单一个体 IP 的增长天花板日益明显。创客匠人提出的 “联盟生态思维”&#xff0c;正推动家庭教育行业从 “单打独斗” 转向 “矩阵作战”&#xff0c;其核心在于通过工具整合资源&#xff0c;将 “同行竞争” 转化为 “…

【Docker基础】Docker容器管理:docker stop详解

目录 1 Docker容器生命周期概述 2 docker stop命令深度解析 2.1 命令基本语法 2.2 命令执行流程 2.3 stop与kill的区别 3 docker stop的工作原理 3.1 工作流程 3.2 详细工作流程 3.3 信号处理机制 4 docker stop的使用场景与最佳实践 4.1 典型使用场景 场景1&#…

rules写成动态

拖拽排序和必填校验联动(rules写到computed里) computed: {rules() {const rules {};this.form.feedList.forEach((item, idx) > {rules[feedList.${idx}] [{ required: true, message: 路线评价动态${idx 1}待填写&#xff0c;请填写完毕提交, trigger: change }];});re…