qt pyside - qsql*model、qabstractitemmodel 和 qtreeview 交互
我想生成一个足够简单的应用程序,它使用 QTreeView 小部件显示 SQLite3(平面)表中的分层数据,使用 QDataWidgetMapper 填充一些行编辑字段,允许用户编辑,从而更新表。简单&基本(对于大多数人来说!)。
我一直在努力,以下过程将是执行此操作的最佳方法:
- 连接到数据库
- 查询数据
- 从数据创建并填充自定义 QAbstractItemModel(通过
dict
操作它来创建节点,动态的父级和子级 - 对于每个字典条目,都会生成一个带有关联父级的“节点”) - 使用 QDatawidgetmapper 填充其他小部件
- 用户编辑数据
- QAbstractItemModel (QAIM) 更新
- 然后必须运行UPDATE、INSERT 或使用 QAIM 模型中的新值的任何查询。
- 刷新 QAIM 和关联的小部件。
我意识到如果我只是使用 QTableView 或 QListView 我不需要自定义模型,并且可以直接写回数据库。我上面概述的过程似乎意味着必须保持两组数据运行 - 即 SQLite 表和自定义 QAIM,并确保它们都保持最新。这对我来说似乎有点麻烦,我确信一定有更好的方法来做到这一点,其中 QTreeView 直接从 SQLite 表获取数据 - 显然需要一些操作来将平面数据转换为分层数据。
当然,我想知道我是否完全误解了 QAbstractItemModel 和 QSQL*Models 之间的关系,并且由于无知而使其变得过于复杂?
谢谢
I want to produce a simple enough application which uses a QTreeView widget to show hierarchical data from a SQLite3 (flat) table, use QDataWidgetMapper to populate some lineedit fields, allow user to edit, which in turn updates the table. Simple & basic (for most!).
I have been working on the basis that the following process would be the best way of doing this:
- Connect to Dbase
- Query data
- Create and populate custom QAbstractItemModel from the data (manipulating it through a
dict
to create nodes, parents and children dynamically - for each dict entry a 'node' is generated with an associated parent) - Use QDatawidgetmapper to populate other widgets
- User edits data
- QAbstractItemModel (QAIM) is updated
- Then have to run an UPDATE, INSERT or whatever query using new values in the QAIM model.
- Refresh the QAIM and associated widgets.
I realise if I were just using a QTableView or QListView I would not need the custom model and could just write straight back into the database. The process I have outlined above seems to mean having to keep two sets of data going - i.e. the SQLite table and the custom QAIM and ensure that they are both kept up to date. This seems a bit cumbersome to me and I'm sure there must be a better way of doing it where the QTreeView is taking its data straight from the SQLite table - with the obvious need for some manipulation to convert the flat data into hierarchical data.
I am wondering, of course, whether I have completely misunderstood the relationship between QAbstractItemModel and the QSQL*Models and I am overcomplicating it through ignorance?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要的是一个充当 QSql*Model 和视图之间桥梁的代理模型。为此,您需要子类化
QAbstractProxyModel
。您必须采用一致的方法来查找代理模型中的父子关系并将其映射到源模型,因此可能需要在代理模型中保留一些记录。当您子类化
QAbstractProxyModel
时,您至少需要重新定义以下方法:另外,请记住
QAbstractProxyModel
确实不自动传播信号。因此,为了让视图了解源模型中的更改(例如插入、删除、更新),您需要将它们传递到代理模型中(当然,同时更新代理模型中的映射)。这将需要一些工作,但最终您将拥有更灵活的结构。它将消除同步数据库和自定义 QAbstractItemModel 所需执行的所有操作。
编辑
自定义代理模型,根据给定列对平面模型中的项目进行分组:
What you want is a proxy model that acts as a bridge between
QSql*Model
and the view. For that, you need to subclassQAbstractProxyModel
. You have to have a consistent way of finding parent-child relationships in proxy model and mapping them to the source model, so that might require keeping some tally in the proxy model.When you are sub-classing
QAbstractProxyModel
, you need to re-define, at minimum, these methods:Also, keep in mind that
QAbstractProxyModel
does not auto-propagate signals through. So, in order to have the view be aware of changes in source model (like insert, delete, update), you need to pass them in the proxy model (while of course, updating your mappings in the proxy model).It will require some work, but in the end you'll have a more flexible structure. And it will eliminate all the stuff that you need to do for synchronizing database and custom
QAbstractItemModel
.Edit
A custom proxy model that groups items from a flat model according to a given column: