当对象外部更改时更新 QListView

发布于 2024-08-03 14:19:24 字数 753 浏览 12 评论 0原文

当模型对象在外部发生更改时,我有一个关于更新 QTreeView (或 QAbstractItemView 的任何子类))的简单问题。假设一个列表显示了 QAbstractItemModel 的子类,并且该模型的一个项目在列表窗口之外发生了更改,并且我们希望使用更改来更新列表。实现这样的目标通常的策略是什么?我查看了 QAbstractItemModel 的 Qt 文档,并且有一个名为“dataChanged”的信号,当数据从模型的变化来看。但由于这个信号(如所有 QAbstractItemModel 函数/信号/槽)与 QModelIndex 一起使用,它并不像文档明确说明的那样持久,我是否应该以某种方式存储我的数据到 >QPersistenModelIndex(es),所以当我的数据更改时,我将能够找到相应的 QPersistenModelIndex 并将其用作各种 QAbstractItemModel 函数的参数?这就是QPersistentModelIndex(es) 的用途吗?或者我错过了什么?

谢谢。

ps:我想我可以重新加载QTreeView,但是我不知道哪些项目被扩展或哪些被选择。有没有一种策略可以克服这个问题并重新加载列表?

I've a simple question regarding the update of a QTreeView (or any subclass of QAbstractItemView) when a model object changes externally. Let's say that a list shows a subclass of QAbstractItemModel, and an item of that model gets changed outside of the list window, and we would like to update the list with the change. What is the usual strategy to achieve something like this ? I've looked at the Qt documentation of QAbstractItemModel and there is a signal named 'dataChanged' that is (or should be) emited when data from the model changes. But since this signal (as all QAbstractItemModel functions/signals/slots) work with a QModelIndex, which is not persistent as the documentation clearly says, am i supposed to store somehow a mapping of my data to QPersistentModelIndex(es), so when my data change i will be able to find the corresponding QPersistenModelIndex and use that as argument to the various QAbstractItemModel functions ? Is that what QPersistentModelIndex(es) are used for ? Or am i missing something ?

Thank you.

ps: I guess i could just reload the QTreeView, but then i wouldn't know which items were expanded or which were selected. Is there an strategy to overcome this problem and just reload the list ?

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

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

发布评论

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

评论(1

就是爱搞怪 2024-08-10 14:19:24

QTreeView 已经处理了底层模型数据更改的情况(即模型已发出 dataChanged() 信号)。这意味着您不需要在视图上做任何额外的工作。

如果您要实现自己的模型(QAbstractItemView 的派生类),并且要更改模型的内容,则只需发出 dataChanged()< /code> 当您的更改完成时发出信号。信号/槽机制将自动使用该信号通知视图。

QTreeView already handles the case in which the underlying model's data changed (i.e. the model has emitted the dataChanged() signal). That means you don't need to do any additional work on the view.

If you're implementing your own model (a derived class of QAbstractItemView), and you're making a change to the contents of the model, you simply need to emit the dataChanged() signal when your change is complete. The signal/slot mechanism will automatically inform the view using that signal.

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