从 QAbstractTableModel 派生类中删除行不起作用,为什么?
我有一个 Qt 应用程序,我从 QAbstractTableModel 派生了自己的模型类。我已经按照文档中规定的实现了必要的方法。当我调用 removeRows 方法时,我的视图中的更改是正确的(我想要的行删除被删除)。
但不知何故,模型上的操作似乎没有传播到我在模型中用于存储数据的 QList。当我将 QList 中存储的值保存到磁盘时,removeRows 似乎没有删除任何内容。
这是我的removeRows 实现的样子(它基于高级Qt 编程一书,第3 章,第125 页中的代码):
bool MyModel::removeRows(int row, int count, const QModelIndex&)
{
beginRemoveRows( QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
mMyQList.removeAt(row);
}
endRemoveRows();
return true;
}
如何解决这个问题?我错过了什么?
谢谢!
I have a Qt application for which I derived my own model class from QAbstractTableModel. I have implemented the necessary methods as prescribed in the documentation. When I call removeRows method the changes are correct in my View (the rows I wanted to remove are removed).
But somehow, the operations on the model doesn't seem to be propagated to the QList I use in the model to store my data. When I save the values stored in the QList to the disk, it look like nothing was erased from it by removeRows.
Here is what my removeRows implementation looks like (it is based on the code from the book Advanced Qt Programming, Chapter 3, p.125):
bool MyModel::removeRows(int row, int count, const QModelIndex&)
{
beginRemoveRows( QModelIndex(), row, row + count - 1);
for (int i = 0; i < count; ++i) {
mMyQList.removeAt(row);
}
endRemoveRows();
return true;
}
How do I fix this? What did I miss?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Frank O. 所暗示的那样,如果不看到一些代码,就很难知道发生了什么。但从它的声音来看,这些值并没有从 QList 中删除,只是因为您没有将它们取出。当您从 Widget 转移到 Model/View 类时,您必须自己执行此操作。即,在您的removeRows() 方法中,您必须“手动”从QList 中删除行。
Like Frank O. implies, it's hard to know what's going on without seeing some code. But from the sound of it, the values haven't been removed from QList simply because you haven't taken them out. When you move from Widget to Model/View classes, you have to do this yourself. I.e., in your removeRows() method you must remove the rows from the QList 'by hand'.
事实证明我的removeRows实现没有任何问题。
在显示对话框之前,我的单元测试调用了保存方法。该对话框根本没有调用保存方法。
难怪更改在视图中可见,而不是在输出文件中可见......
It turns out that nothing was wrong with my implementation of removeRows.
The save method was called by my unit tests just before showing my dialog. The dialog was not calling the save method at all.
No wonder the change were visible in the View and not in the output file...