qtqmlqt5qstringlistmodel

How to use removeRows with QStringListModel


I have a QStringListModel which works ok, but then I need to delete all the rows from it from QML.

I would expect removeRowsfunction from Qt documentation work like that, but I don't know how to use it property.

removeRows(int row, int count, const QModelIndex &parent = QModelIndex())

I tried to use it like this:

myModel.removeRows(1, 1)

But I am getting thie error:

qrc:/Logger.qml:63: TypeError: Property 'removeRows' of object QStringListModel(0x337350) is not a function

Can someone explain how to use removeRows correctly? Thanks.


Solution

  • removeRows() is not invokable from QML. the solution is to make it invokable by creating a new class and overriding that method:

    class StringListModel: public QStringListModel{
        Q_OBJECT
    public:
        Q_INVOKABLE bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()){
            return QStringListModel::removeRows(row, count, parent);
        }
    };
    

    In the following link there is an example.