qtreeview数据不一致

发布于 2024-12-07 16:06:46 字数 771 浏览 5 评论 0原文

我有一个基于 PyQt 示例中的 editabletreemode.py 的 QTreeView GUI。在模型内部,我根据需要重新实现了 setData():对于某些超出范围的值,我返回 False,否则,它返回 True

def setData(self, index, value, role=QtCore.Qt.EditRole):
    if role != QtCore.Qt.EditRole:
        return False

    item = self.getItem(index)
    result = item.setData(index.column(), value)

    if result:
        self.dataChanged.emit(index, index)
        self.modified = True

    print "setData() returning ", result
    return result

问题是,即使 setData 返回 False,GUI 仍然接受更改的值价值。所以我现在模型和视图之间的数据不一致。对我来说有意义的是,当 setData() 返回 False 来拒绝该值时,视图应该恢复为旧值。这可能吗?

[已解决] 实际上setData()的返回值似乎并不重要。无论如何,QTreeView 似乎都会调用 data() 来重新检索值。我遇到的问题是因为 setData() 更改了内部数据,即使它返回 False。

如果有人可以向我解释 setData() 的返回值的用途是什么,那就太好了。

I have a QTreeView GUI based on editabletreemode.py in the PyQt examples. Inside the model, I re-implemented setData() for my need: for some out-of-bound value, I'm returning False, otherwise, it returns True

def setData(self, index, value, role=QtCore.Qt.EditRole):
    if role != QtCore.Qt.EditRole:
        return False

    item = self.getItem(index)
    result = item.setData(index.column(), value)

    if result:
        self.dataChanged.emit(index, index)
        self.modified = True

    print "setData() returning ", result
    return result

Problem is, even when setData is returning False, GUI still accepts the changed value. So I now have inconsistent data between the model and view. What would make sense to me is that when setData() return False to reject the value, view should revert back to old value. Is this possible?

[SOLVED]
Actually the return value of setData() doesn't seem to matter. The QTreeView seem to call data() to re-retrieve the value afterwards anyway. The problem I was having was because setData() changed the internal data even though it returned False.

If somebody could explain to me what is the return value of setData() is used for, that'd be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

关于从前 2024-12-14 16:06:46

我能看到的唯一问题是 .setData() 的签名,即 .setData(index, value, role)。您的代码片段和问题的描述都不够冗长,无法说明任何其他内容。

编辑:确实,在查看了 Qt 源代码之后,我得到了纠正。与我之前所说的编辑不同,.setData() 的返回值实际上并未被视图使用。

数据由委托的 .setModelData() 提交给模型。通常,Qt 使用 QStyledItemDelegate,其 .setModelData() 方法实际上忽略 .setData() 的返回值。因此视图确实不关心数据是否设置成功。当视图中单元格的编辑器关闭时,视图仅显示该单元格现在的值(由 .data() 检索)。

然而,.setData() 的返回值仍然很重要,表现良好的模型应该注意返回正确的值。模型通常抽象数据源,并且本身独立于视图。因此模型也是直接访问的,在这种情况下,调用者需要检查.setData()的返回值来知道操作是否成功。

The only issue I can see is the signature of .setData(), which is .setData(index, value, role). Neither your code snippet nor the description of your problem are verbose enough to say anything else.

Edit: Indeed, after looking at the Qt sources, I stand corrected. Unlike I've stated before this edit, the return value of .setData() is not actually used by the view.

The data is committed to the model by .setModelData() of the delegate. Normally, Qt uses a QStyledItemDelegate, whose .setModelData() method actually ignores the return value of .setData(). Thus the view does indeed not care for whether the data was successfully set. When the editor for a cell in the view is closed, the view just displays whatever is now the value of that cell (as retrieved by .data()).

However, the return value of .setData() is still important, and well-behaving models should take care to return a proper value. Models generally abstract data sources, and are in and by themselves independent from views. Thus models are also accessed directly, and in this case, the caller needs to check the return value of .setData() to know, whether the operation was successful.

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