Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

incremental data fetching logic distorted when resetting the model



  • i have implemented incremental data fetching for my tree view class.
    at some point in time i need to clear the tree view content:

    void TreeModel::resetModel()
    {
    	beginResetModel();
    	getRootNode()->clear();
    	endResetModel();
    }
    

    where:

    void TreeNode::clear()
    {
    	qDeleteAll(m_children);
    	m_children.clear();
    }
    

    however, my tree view is not cleared because after removing the children the root node still exists, and my canFetchMore() returns true and tree view is filled again.

    i'm thinking of keeping a bool to determine whether or not to return true or false from canFetchMore() but i don't think that's a good solution. what else can i do?

    by the way:

    bool TreeModel::canFetchMore(const QModelIndex &parent) const
    {
    	if (!parent.isValid())
    		return m_pRootNode->childCount() < m_pRootNode->actualChildCount();
    
    	// get the node pointer
    	auto node = static_cast<TreeNode *>(parent.internalPointer());
    	return node->childCount() < node->actualChildCount();
    }
    


  • @user4592357 said in incremental data fetching logic distorted when resetting the model:

    and my canFetchMore() returns true

    This is the problem, nothing to do with the view.

    I assume getRootNode() returns m_pRootNode? if so can you show the code for childCount and actualChildCount?



  • @VRonin
    yes that's right.

    int TreeNode::childCount() const
    {
    	return m_children.size();
    }
    
    int TreeNode::actualChildCount() const
    {
    	return m_nActualChildCount;
    }
    

    and m_nActualChildCount is calculated once



  • @user4592357 said in incremental data fetching logic distorted when resetting the model:

    and m_nActualChildCount is calculated once

    This needs to be recalculated if you reset the model


Log in to reply