Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QAbstractItemModel::endInsertRows message in terminal
QtWS25 Last Chance

QAbstractItemModel::endInsertRows message in terminal

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 578 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    user4592357
    wrote on last edited by
    #1

    i have implemented incremental loading for tree view. when a new batch is being fetched, i get the following message in terminal, and the rest of node children do not load:
    QAbstractItemModel::endInsertRows: Invalid index ( 123 , 0 ) in model QAbstractItemModel(0xwhatever)
    here are relevant functions from tree model:

    bool TreeModel::canFetchMore(const QModelIndex &parent) const
    {
    	if (!parent.isValid())
    	{
    		// return whether mode data is available
    		return m_pRootNode->childCount() < m_pRootNode->actualChildCount();
    	}
    
    	// get the node pointer
    	auto node = static_cast<TreeNode *>(parent.internalPointer());
    	return node->childCount() < node->actualChildCount();
    }
    
    void TreeModel::fetchMore(const QModelIndex &parentIdx)
    {
    	// get the node pointer
    	auto node = !parentIdx.isValid()
    		? m_pRootNode.get()
    		: static_cast<TreeNode *>(parentIdx.internalPointer());
    	if (!node) return;
    
    	// get the number of items to fetch
    	const auto nChildCount = node->childCount();
    	int itemsToFetch = qMin(node->childBatchCount(), node->actualChildCount() - nChildCount);
    	if (itemsToFetch <= 0) return;
    
    	// fetch the data from the node
    	beginInsertRows(parent(parentIdx), nChildCount, nChildCount + itemsToFetch - 1);
    	node->fetchItems(itemsToFetch);
    	endInsertRows();
    }
    
    bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
    {
    	if (!index.isValid())
    		return false;
    
    	// get the node pointer
    	auto node = static_cast<TreeNode *>(index.internalPointer());
    
    	bool bUpdated = true;
    	if (role == Qt::DisplayRole)
    	{
    		QList<QVariant> data;
    		data << value;
    		node->setColumnsData(data);
    	}
    	else if (role == Qt::CheckStateRole)
    	{
    		bUpdated = node->setProperty(index.column(), value);
    	}
    	else
    		bUpdated = false;
    
    	// emit the dataChanged() signal
    	if (bUpdated)
    	{
    		auto parentIndex = parent(index);
    		auto topLeftIndex = this->index(0, 0, parentIndex);
    		auto bottomRightIndex = this->index(
    			rowCount(parentIndex) - 1, columnCount(parentIndex) - 1, parentIndex);
    		emit dataChanged(topLeftIndex, bottomRightIndex);
    	}
    
    	return bUpdated;
    }
    
    void TreeModel::insertChild(TreeNode *parent, TreeNode *child, int row /* = -1 */)
    {
    	// get the parent node's model index
    	auto parentIndex = index(parent);
    
    	// adjust row if necessary
    	if (row == -1)
    		row = parent->childCount();
    
    	// let the model know we're making modifications
    	beginInsertRows(parentIndex, row, row);
    	parent->addChild(child, row);
    	endInsertRows();
    
    	// update the model
    	updateModel();
    }
    

    and from node:

    void TreeNode::fetchItems(int itemsToFetch)
    {
    	QSqlQuery sqlQuery(QSqlDatabase::database(getPath()));
    	sqlQuery.prepare(sQueryString).arg(itemsToFetch/* limit */).arg(childCount()/* offset */));
    
    	sqlQuery.bindValue(key, value);
    	if (!sqlQuery.exec()) return;
    
    	while (sqlQuery.next())
    	{
    		// create a node
    		addChild(new ChildNode(sqlQuery.value(0).toString(), this));
    	}
    }
    

    the node is populated with only 60 items (20 is the batch fetch count), and the number of total items is 129, but the error reports 123.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Might be a silly question but are you sure that you are getting as much data as you think from the database ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • U Offline
        U Offline
        user4592357
        wrote on last edited by user4592357
        #3

        i executed the query on the database and yes, i do get the 129 results.
        i don't get why it's reporting 123, when i only have 60 child nodes currently

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Just add some debug output to see what really happens / how often the stuff is called.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          U 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            Just add some debug output to see what really happens / how often the stuff is called.

            U Offline
            U Offline
            user4592357
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            i did. found out after inserting 60th child node from fetchMore, endInsertRows prints the message.

            i looked at implementation of rowsInserted but i don't see how it could get from 60 to 123.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              user4592357
              wrote on last edited by user4592357
              #6

              EDIT: i found this issue, beginInsertRows should take parent index, not parent of parent.

              i found more details on the issue.
              so i have two level tree view:

              a
              |_a1
              |_a2
              |_...
              b
              |_b2
              

              the problem occurs when i expand a first level node, e.g. a, and i get the message when the expanded node happens to be near the bottom.
              when i moved the same node near top, there's no message in terminal.
              so i guess the 123 i'm getting actually refers to first level item population, and not for expanded node.
              but see, it says ( 123 , 0 ) i.e. first column, so why is it happening when i expand the node?
              seems it is trying to insert expanded node's children as first level items, but first level items (rowCount()) are less than 123

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved