C++ 在filterAcceptsRow中对其调用size()后,std::vector无效
我正在使用自制的QSortFilterProxyModel对应用程序中的模型进行过滤和排序 以下是filterAcceptsRow函数:C++ 在filterAcceptsRow中对其调用size()后,std::vector无效,c++,qt,model-view-controller,stl,qt-creator,C++,Qt,Model View Controller,Stl,Qt Creator,我正在使用自制的QSortFilterProxyModel对应用程序中的模型进行过滤和排序 以下是filterAcceptsRow函数: //--------------------------------------------------------------------------------- bool SkillSortFilterProxyModel::filterAcceptsRow(int p_row, const QModelIndex& p_parent) cons
//---------------------------------------------------------------------------------
bool
SkillSortFilterProxyModel::filterAcceptsRow(int p_row, const QModelIndex& p_parent) const
{
QModelIndex currentIndex = sourceModel()->index(p_row, 0, p_parent);
SkillModelItem* currentItem = (SkillModelItem*)currentIndex.internalPointer();
// Check the item
bool accept = filterAcceptsItem(currentItem);
// We may hide a category if it is empty
if (accept && currentItem->isCategory && !_showEmptyCategories)
{
// Iterate over the children to find out if any will be accepted
// And if so, accept the category
int numChildren = currentItem->children.size();
accept = false;
for (int i = 0; i < numChildren; ++i)
{
SkillModelItem* item = currentItem->children[i];
if (filterAcceptsItem(item))
{
accept = true;
break;
}
}
}
// Return result
return accept;
}
//---------------------------------------------------------------------------------
布尔
SkillSortFilterProxyModel::filterAcceptsRow(int p_行,常量QModelIndex&p_父级)常量
{
QModelIndex currentIndex=sourceModel()->index(p_行,0,p_父级);
SkillModelItem*currentItem=(SkillModelItem*)currentIndex.internalPointer();
//检查项目
bool accept=filterAcceptsItem(当前项);
//如果类别为空,则可以隐藏该类别
如果(接受和当前项->isCategory和!\u showEmptyCategories)
{
//迭代子项以确定是否接受任何子项
//如果是,请接受该类别
int numChildren=currentItem->children.size();
接受=错误;
for(int i=0;ichildren[i];
if(过滤器接收站点M(项目))
{
接受=真;
打破
}
}
}
//返回结果
退货接受;
}
这里是它应该做的:我认为SkillModelItem的确切性质在这里并不重要,但是您应该理解,相同的模型item类用于技能类别和技能本身。filterAcceptsRow函数调用filterAcceptsItem以查看是否应显示特定项。这很有效
但是,如果项目是一个类别,则还应检查其子项以查看是否有任何接受的子项,如果有,则应显示该类别
理论上应该是可行的,但实际情况是调用currentItem->children.size()后,currentItem->children(这是一个std::vector)将无效!它返回正确的大小,但是如果我再次调用它,大小现在是一些随机数。以及在应用程序崩溃后访问for循环中的子项
我不知道这里发生了什么。应用程序没有线程(至少我没有使用任何线程)。我在Windows上使用QtCreator,使用MinGW作为编译器。我也尝试过使用MSVC,但它甚至无法编译,因为它声称它找不到任何头文件(MinGW可以毫无问题地找到)。还尝试了重新编译、重新运行qmake等,但均无效
你知道这里有什么问题吗
如果有帮助,您可以查看这里的源代码:这是一个调试器问题 如果我在运行时不单步执行,该函数就可以正常工作 似乎Qt Creator或附带的MinGW版本中都存在缺陷