QModelIndex::child() 源码
时间: 2024-05-02 09:18:00 浏览: 16
QModelIndex::child() 的源码如下:
```c++
QModelIndex QModelIndex::child(int row, int column) const
{
if (row < 0 || column < 0 || row >= model()->rowCount(*this) || column >= model()->columnCount(*this))
return QModelIndex();
return model()->index(row, column, *this);
}
```
这个函数用于返回指定行号和列号的子项的索引。如果行号或列号越界,则返回一个无效的 QModelIndex 对象。
此函数首先判断行号和列号是否合法,如果不合法则返回一个无效的 QModelIndex 对象。如果行号和列号都合法,则调用 model() 函数获取模型对象,然后调用模型对象的 rowCount() 和 columnCount() 函数获取行数和列数。如果指定的行号和列号都小于行数和列数,则调用模型对象的 index() 函数获取指定行号和列号的子项索引。最后将获取到的子项索引返回给调用者。
相关问题
QModelIndex::Index() 源码
QModelIndex::Index() 源码如下:
```cpp
QModelIndex::QModelIndex() : d(nullptr)
{
}
QModelIndex::QModelIndex(const QModelIndex &other) : d(other.d)
{
}
QModelIndex::QModelIndex(int row, int column, void *data) : d(new QPersistentModelIndexPrivate(row, column, data))
{
}
QModelIndex::~QModelIndex()
{
}
QModelIndex &QModelIndex::operator=(const QModelIndex &other)
{
d = other.d;
return *this;
}
bool QModelIndex::operator==(const QModelIndex &other) const
{
return d == other.d;
}
bool QModelIndex::operator!=(const QModelIndex &other) const
{
return d != other.d;
}
bool QModelIndex::isValid() const
{
return d != nullptr && d->model != nullptr && d->model->d_func()->indexIsValid(d->internalId);
}
int QModelIndex::row() const
{
return d ? d->row : -1;
}
int QModelIndex::column() const
{
return d ? d->column : -1;
}
QVariant QModelIndex::data(int role) const
{
return d ? d->model->data(*this, role) : QVariant();
}
QModelIndex QModelIndex::child(int row, int column) const
{
return d ? d->model->index(row, column, *this) : QModelIndex();
}
QModelIndex QModelIndex::parent() const
{
return d ? d->model->parent(*this) : QModelIndex();
}
QModelIndex QModelIndex::sibling(int row, int column) const
{
return d ? d->model->sibling(row, column, *this) : QModelIndex();
}
void *QModelIndex::internalPointer() const
{
return d ? d->internalPointer : nullptr;
}
qint64 QModelIndex::internalId() const
{
return d ? d->internalId : 0;
}
```
注:此处源码来自 Qt 5.15.2 版本。
qt 6 中QModelIndex::Index() 源码
在Qt 6中,QModelIndex::Index()函数的源码如下:
```cpp
int QModelIndex::Index() const
{
return d ? d->index : 0;
}
```
该函数返回索引值,该值是一个整数,用于标识模型中的项。该值在模型中具有唯一性,并且可以用于查找特定项。
该函数的实现非常简单,它首先检查QModelIndex对象是否为有效对象,如果是,则返回QModelIndexPrivate结构中存储的索引值。否则,返回0。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)