从 QAbstractTableModel 派生类中删除行不起作用,为什么?

发布于 2024-10-26 14:18:12 字数 873 浏览 4 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-11-02 14:18:12

正如 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'.

毁梦 2024-11-02 14:18:12

事实证明我的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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文