如何将removeRows与QStringListModel一起使用

时间:2017-09-22 14:50:41

标签: qt qml qt5 qstringlistmodel

我有一个QStringListModel可以正常工作,但我需要删除它中的所有行来自QML

我希望Qt文档中的removeRowsfunction能像这样工作,但我不知道如何使用它。

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

我试着像这样使用它:

myModel.removeRows(1, 1)

但是我得到了错误:

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

有人可以解释如何正确使用removeRows吗?感谢。

1 个答案:

答案 0 :(得分:3)

removeRows()不能从QML中调用。解决方案是通过创建一个新类并重写该方法使其可调用:

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);
    }
};

在下面的link中有一个例子。