我使用的是自制QSortFilterProxyModel做過濾和我的應用程序模型的排序後無效。的std ::向量變得調用它的尺寸()內filterAcceptsRow
這裏是filterAcceptsRow功能:
//---------------------------------------------------------------------------------
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;
}
這裏是什麼是應該做的事:我認爲SkillModelItem的確切性質並不重要,但你應該明白,同型號商品類用於技能類別和技能本身。 filterAcceptsRow函數調用filterAcceptsItem來查看是否應顯示特定項目。這很好。
但是,如果該項目是一個類別,它的子項也應該被檢查以確定它是否有任何被接受的子項,如果是,則應該顯示該項目類別。
在理論上應該工作,但是發生的事情在實踐中,經過currentItem-> children.size()被調用時,currentItem->孩子(這是一個std ::向量)無效!它返回正確的大小,但如果我再次調用它,大小現在是一個隨機數。並在崩潰應用程序後訪問for循環中的子項。
我不知道這裏發生了什麼。該應用程序不是線程的(至少我不使用任何線程)。我在Windows上使用Qt Creator,使用MinGW作爲編譯器。我也嘗試使用MSVC,但它甚至不會編譯,因爲它聲稱它找不到任何頭文件(MinGW可以毫無問題地找到它)。也試過重新編譯,重新運行qmake等等都無濟於事。
任何想法可能是這裏的問題?
如果有幫助,你可以看看這裏的來源:GitHub repo