QStandardItemModel 获取子模型
QStandardItemModel get model of a child
我是Qt编程的初学者,我有一个小问题。事实上,我有很大的QStandardItemModel
,我需要找到一些带有关键字的特定项目。这个想法是让用户给出两个输入,一个用于国家,另一个用于城市。找到国家后,只需在匹配的国家/地区下搜索该城市。但是底层代码,它一直在搜索整个树模型。
为了获得匹配的国家/地区,我这样做:
foundCountriesList = TreeModel->findItems(countryKeyword,
Qt::MatchStartsWith | Qt::MatchFixedString | Qt::MatchRecursive, 0);
然后,我只需要在匹配的国家/地区内找到city
关键字:
if (!foundCountriesList.isEmpty())
{
foreach(QStandardItem* item, foundCountriesList)
{
foundCitiesList = item->child(0,0)->Model()->findItems(cityKeyword,
Qt::MatchStartsWith | Qt::MatchFixedString |
Qt::MatchRecursive, 0);
}
}
但是,它一直在寻找整个TreeModel
city
,因为每当我做TreeModel->Item(0,0)->child(0,0)->Model()
时,我总是会得到TreeModel
。
谁能给我一些提示?
提前谢谢你!
我会通过以下方式解决它:
QStandardItem *findCityItem(const QString &city, const QString &country)
{
auto cityItems = TreeModel->findItems(city,
Qt::MatchRecursive | Qt::MatchWrap | Qt::MatchExactly, 0);
for (auto cityItem : cityItems)
{
auto parent = item->parent();
if (parent && (parent->data().toString() == country))
{
return item;
}
}
return nullptr;
}
即搜索城市名称,如果找到城市,请检查它们属于哪个国家。
由于您已经遍历了具有所需国家/地区的所有项目,因此您可以通过检查项目的值自行过滤掉城市。
您也可以尝试使用QSortFilterProxyModel。制作一个按国家/地区过滤掉(它的源将是你的主模型(,另一个按城市过滤掉(它的源将是国家/地区的代理模型(。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- Qt - QVector 和模型视图 - 从列表视图获取自定义类的最佳方法是什么?
- C++内存模型中的确切规则阻止在获取操作之前重新排序
- 如何从TensorFlow .pb模型中获取权重格式?
- QStandardItemModel 获取子模型
- 如何从模型/视图中获取QImage POINTER
- 如何在OpenGL或Win32中获取图形卡模型名称
- 从委托C++模型中获取数据
- QMatrix4x4 模型视图投影 OpenGL 无法获取要渲染的场景
- 如何从从表/模型中获取值的 QML 组合框中访问值
- 在C++中部署GBM模型|获取Predict.GBM以在R之外工作
- 使用二维屏幕坐标gluUnproject获取三维模型坐标
- 如何在Qt中获取QSortFilterProxyModel的源模型
- 获取模型坐标,在 OpenGL 中返回错误的值
- 如何获取具有两列模型的QComboBox的当前值
- 正在从服务器客户端模型获取提要
- 在opengl中从模型中获取可见的XYZ坐标和法线
- 在QML中通过索引从c++模型中获取数据
- 模型视图-在Opengl2.x[C++]中获取模型视图和投影矩阵
- 移除移动物体,从多个图像中获取背景模型