(1)本类型也支持调试的打印输出 :

(2)本持久索引的功能和 QModelIndex 非常相似,不再测试其成员函数了 ,给出源码 ,同样来自于 qabstractitemmodel . h 头文件:
class QPersistentModelIndexData;
// qHash is a friend, but we can't use default arguments for friends (§8.3.6.4)
size_t qHash(const QPersistentModelIndex & index, size_t seed = 0) noexcept;
/*
The QPersistentModelIndex class is used to locate data in a data model.
A QPersistentModellndex是一种模型索引,可由应用程序存储,并随后用于访问模型中的信息。
与QModellndex类不同,存储 QPersistentModelndex是安全的,
因为模型将确保对项的引用在模型能够访问它们的时间内保持有效。
A QPersistentModelIndex is a model index that can be stored by an application,
and later used to access information in a model.
Unlike the QModelIndex class, it is safe to store a QPersistentModelIndex since the
model will ensure that references to items will continue to be valid as long as
they can be accessed by the model.
在使用持久化模型索引之前,检查它们的有效性是良好的做法。
It is good practice to check that persistent model indexes are valid before using them.
注意:您不能将QStandardltemModel的QPersistentModellndex存储在模型的一个项目中。
Note: You cannot store a QStandardItemModel's QPersistentModelIndex
in one of the model's items.
*/
class Q_CORE_EXPORT QPersistentModelIndex
{
private: //关于数据成员,明显不同于 QModelIndex。
//class QModelIndex { int r, c; quintptr i; QAbstractItemModel * m; };
QPersistentModelIndexData * d;
friend
size_t qHash(const QPersistentModelIndex & , size_t seed) noexcept;
friend
bool qHashEquals(const QPersistentModelIndex & a,
const QPersistentModelIndex & b) noexcept
{ return a.d == b.d; }
friend Q_CORE_EXPORT
QDebug operator<<(QDebug, const QPersistentModelIndex &);
public:
QPersistentModelIndex(); //默认构造函数
//Creates a new QPersistentModelIndex that is a copy of the model index.
QPersistentModelIndex(const QModelIndex & index); //有参构造函数
//Creates a new QPersistentModelIndex that is a copy of the
// other persistent model index.
QPersistentModelIndex(const QPersistentModelIndex & other); //copy构造函数
QPersistentModelIndex & operator=(const QPersistentModelIndex & other);//copy赋值运算符函数
//Move-constructs a QPersistentModelIndex instance,
// making it point at the same object that other was pointing to.
inline
QPersistentModelIndex(QPersistentModelIndex && other) noexcept //移动构造函数
: d(qExchange(other.d, nullptr)) {}
QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPersistentModelIndex) //移动赋值运算符函数
~QPersistentModelIndex(); //析构函数
QPersistentModelIndex & operator=(const QModelIndex & other); //赋值运算符函数
bool operator==(const QPersistentModelIndex & other) const; //等于比较运算符
bool operator==(const QModelIndex & other) const; //等于比较运算符重载
inline
bool operator!=(const QPersistentModelIndex & other) const //不等比较运算符函数
{ return !operator==(other); }
bool operator!=(const QModelIndex & other) const; //不等比较运算符重载
bool operator< (const QPersistentModelIndex & other) const; //小于< 运算符比较
operator QModelIndex() const; //类型转换运算符函数,说明本类可以转换为 QModelIndex 类型
//Swaps this persistent modelindex with other.
//This function is very fast and never fails.
void swap(QPersistentModelIndex & other) noexcept
{ qt_ptr_swap(d, other.d); }
//Returns true if this persistent model index is valid; otherwise returns false.
//A valid index belongs to a model, and has non-negative row and column numbers.
bool isValid() const;
Qt::ItemFlags flags() const;//Returns the flags for the item referred to by the index.
//Returns the row / column this persistent model index refers to.
int row () const;
int column () const;
quintptr internalId() const;
const //Returns the model that the index belongs to.
QAbstractItemModel * model () const;
void * internalPointer() const;
const void * constInternalPointer() const;
//Returns the data for the given role for the item referred to by the index.
QVariant data(int role = Qt::DisplayRole) const;
void multiData(QModelRoleDataSpan roleDataSpan) const;
//Populates the given roleDataSpan for the item referred to by the index.
//Returns the parent QModelIndex for this persistent index,
// or an invalid QModelIndex if it has no parent.
QModelIndex parent() const; //返回本持久索引指向的条目的父节点对应的索引。
QModelIndex sibling(int row, int column) const;
//Returns the sibling at row and column or an invalid QModelIndex
// if there is no sibling at this position.
}; //完结 class QPersistentModelIndex
Q_DECLARE_SHARED(QPersistentModelIndex)
inline size_t qHash(const QPersistentModelIndex &index, size_t seed) noexcept
{ return qHash(index.d, seed); }
Q_CORE_EXPORT QDebug operator<<(QDebug, const QPersistentModelIndex &);
(3)
谢谢
2443

被折叠的 条评论
为什么被折叠?



