在已经实例化的 QAbstractListModel 子类中,如何在每列中添加包含数据的行,并让关联的 QListView 显示新行?
似乎唯一的方法是在我的模型中重新实现 insertRow 和 setData,然后在另一个函数中以某种顺序将它们组合在一起以添加一行。我必须这样做吗?当然,Qt 必须使添加新行变得更容易。
非常感谢!
——丹妮。
最佳答案
只需在 beginInsertRows() 和 endInsertRows() 之间更改模型的数据存储。
例如,假设您有一个平面列表模型,并且您的模型将数据内部存储在 QVector m_data 中。您想添加列表,即在位置 0 插入一行:
beginInsertRows( QModelIndex(), 0, 0 ); //notify views and proxy models that a line will be inserted
m_data.prepend( somedata ); // do the modification to the model data
endInsertRows(); //finish insertion, notify views/models
关于qt - 以编程方式向 QAbstractListModel 子类添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702972/