With QAbstractListModel, is it necessary to implement insertRows and removeRows if the model is resizable?

huangapple go评论135阅读模式
英文:

With QAbstractListModel, is it necessary to implement insertRows and removeRows if the model is resizable?

问题

我正在实现一个派生自QAbstractListModel的类(尽管这个问题也适用于QAbstractItemModel),它将作为QListView的模型。这个列表视图旨在作为一个带有最大显示项目数量限制的日志。因此,它具有以下属性:

  • 项目不可编辑
  • 不能拖放或以其他方式重新排序
  • 行的添加和删除不是由用户操作驱动的(即,有一个新项目要记录)

基本上,我的模型将具有类似于appendItem(const MyDataType &val)的函数,根据列表中的项目数量,追加一个项目可能会从列表顶部删除一个项目。

在这种情况下,是否需要重新实现QAbstractListModel中的insertRows()removeRows()?无论如何,这些函数似乎都不适用于我正在做的事情,因为insertRows() 指定正在添加的数据,然而我需要对我的模型的底层数据结构进行更改以添加一个项目。只有在我有实际数据要添加时才有意义。

我是否可以只编写一个像appendItem(const MyDataType &val)这样的函数,内部调用beginInsertRowsendInsertRows,如果必要,调用beginRemoveRowsendRemoveRows,而不用担心insertRowsremoveRows?或者这样使用QAbstractItemModel是错误的,会导致错误?

英文:

I'm implementing a QAbstractListModel-derived class (though this question can apply to QAbstractItemModel as well) that will be a QListView's model. The list view is intended to be a log with a maximum number of items that it can display. So it has the following properties:

  • The items are not editable
  • They can't be dragged, dropped, or otherwise reordered
  • Rows are added and removed by a process not driven by user action (i.e. there's a new item to log)

Basically, my model will have something akin to an appendItem(const MyDataType &val) function, and depending on the number of items in the list, appending an item might remove an item from the top of the list.

In this case, is it necessary to reimplement insertRows() and removeRows() from QAbstractListModel? These functions seem ill-suited for what I'm doing anyway, because insertRows() does not specify what data is being added, and yet I need to make a change to my model's underlying data structure to add an item. That only makes sense to do if I have actual data to add.

Can I just write a function like appendItem(const MyDataType &val) that internally calls beginInsertRows, endInsertRows, and if necessary, beginRemoveRows and endRemoveRows, and not bother with insertRows and removeRows? Or is that using QAbstractItemModel incorrectly and will cause errors?

答案1

得分: 1

The appendItem(...) approach is valid. It is exactly akin to void QStandardItemModel::appendRow(const QList<QStandardItem *> &items) which is nothing more than a method created by the Qt developers themselves solely to make life easier. If it is something they do, why not you?

Note that as soon as you make your model writable from a view, you need to override all these pesky methods from QAbstractItemView but even in that case, you are free to keep appendItem(...) as a convenience method.

英文:

The appendItem(...) approach is valid.<br/>
It is exactly akin to void QStandardItemModel::appendRow(const QList&lt;QStandardItem *&gt; &amp;items) which is nothing more than a method created by the Qt developers themselves solely to make life easier. If it is something they do, why not you?

Note that as soon as you make your model writable from a view, you need to override all these pesky methods from QAbstractItemView but even in that case, you are free to keep appendItem(...) as a convenience method.

huangapple
  • 本文由 发表于 2023年8月10日 23:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76877240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定