QStandardItemModel 遍历
时间: 2024-05-17 09:18:49 浏览: 27
要遍历一个 QStandardItemModel,你可以使用 QStandardItemModel 的 rowCount() 和 columnCount() 方法来获取行数和列数,然后使用 index() 方法来获取每个单元格的 QModelIndex。
下面是一个示例代码来遍历一个 QStandardItemModel:
```cpp
QStandardItemModel* model = new QStandardItemModel();
// 添加数据到 model
int rowCount = model->rowCount();
int columnCount = model->columnCount();
for (int row = 0; row < rowCount; ++row) {
for (int column = 0; column < columnCount; ++column) {
QModelIndex index = model->index(row, column);
QVariant data = model->data(index);
// 处理数据
qDebug() << "Row:" << row << "Column:" << column << "Data:" << data.toString();
}
}
```
在循环中,我们使用 index() 方法获取每个单元格的 QModelIndex,然后使用 data() 方法获取单元格的数据。你可以根据需要处理每个数据项。
请注意,这只是一个简单的示例,你需要根据你的实际情况进行适当的修改。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)