简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM