gpt4 book ai didi

qt - 如何将 removeRows 与 QStringListModel 一起使用

转载 作者:行者123 更新时间:2023-12-04 02:02:19 24 4
gpt4 key购买 nike

我有一个工作正常的 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 吗?谢谢。

最佳答案

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有一个例子。

关于qt - 如何将 removeRows 与 QStringListModel 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46367568/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com