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

How to remove and insert rows in QAbstractItemModelReplica from qml



  • I don't understand how to remove and insert items from my ListView if the model is a QAbstractItemModelReplica. The functions insertRows and removeRows are not Q_INVOKABLE.

    Do I have to derive from QAbstractItemModelReplica and overwrite these two functions like below? And do I have to call beginInsertRows and endInsertRows in the replica or the source part of the model?

    class MyModelReplica : public QAbstractItemModelReplica 
    {
        ...
    
        Q_INVOKABLE bool insertRows(int row, int count, const QModelIndex& parent = {}) override 
        {
            beginInsertRows(parent, row, row + count - 1);
            QAbstractItemModelReplica::insertRows(row, count, parent);
            endInsertRows();
        }
    
        Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex& parent = {}) override
        {
            beginRemoveRows(parent, row, row + count - 1);
            QAbstractItemModelReplica::removeRows(row, count, parent);
            endRemoveRows();
        }
    }
    

    Or is there another way?


Log in to reply