Important: Please read the Qt Code of Conduct -

Removing row from QAbstractItemModel

  • Hi all,

    I'm using QML treeview with model sub-classed from QAbstractItemModel.
    I'm facing this issue that model indexes aren't updated when user removes a row.

    Here's the code

        bool TreeModel::removeRows(int row, int count, const QModelIndex &parent)
        	TreeItem *parentItem = getItem(parent);
        if(parentItem == null) return false;
        	bool success = false;
        	if (count > 0)
        		beginRemoveRows(parent, row, row + count - 1);
        		success = parentItem->removeChildren(row, count);
        	return success;
        bool TreeItem::removeChildren(int position, int count)
        	if (position < 0 || position + count > children_.size())
        		return false;
        	for (int row = 0; row < count; ++row)
        		delete children_.takeAt(position);
        	return true;

    Could you point what am I missing?

  • Hi,

    I have exactly the same issue. The ony way I could solve it is to call beginResetModel and endResetModel after I deleted the rows.

    It seems to me like a bug in the TreeView / ItemDelegate implementation because this works using a C++ QTreeView.

Log in to reply