Qt如何跨表呈现和编辑数据
请耐心等待,因为这是一个有点主观的问题,但我正在寻求针对可能常见情况的最佳实践。
我将数据分成多个(sqlite)表,我使用 Qt 来操作这些表。 (sql) 视图是显示多个表中的数据的一种方法,但我也希望能够更新值。 让我们举一个三表的例子,使用房屋。 这三个表通过外键链接,例如区域表、房屋表和家具表。存在与外键关联的一对多关系:Area:House、House:Furniture 我想要一个带有区域的 UI 项目填充一个 House UI 项目,而 House UI 项目又填充一个 Furniture UI 项目。通过 UI 项目,我的意思是诸如可编辑列表或一系列 lineEdits/Spinners(取决于值类型)之类的东西。
只是为了澄清一点,我了解如何使用 qsqlrelationaltablemodel 和 qsqlrelationdelegate。当我更新单个表时,这些工作正常,但不会更新从多个表上的联接创建的 (sql) 视图。 我正在寻找一种解决方案,可以让我有效地处理多个表中的数据,包括更新值。
我应该对三个模型执行此操作,更新每个模型的过滤器,并使用链接到每个模型的视图吗? 或者也许我可以将所有内容插入到单个树模型中? 或者我应该考虑创建一个自定义模型,或者只是不使用模型/视图并手动填充 UI?
不久前,我尝试在同一个数据库上使用多个模型作为实验,但它们的表现并不像我希望的那样好。这是我询问最佳实践的主要原因。
非常感谢您提供的任何建议。
Please bear with me as this is a slightly subjective matter but I'm after informed best practice for what is likely a common situation.
I have data split out into multiple (sqlite) tables which I am using Qt to manipulate. A (sql) view is one way to show data from multiple tables, but I wish to be able to update values as well.
Let's take a three table example, using say houses.
The three tables are linked via foreign keys, say an Area table, a House table, and a Furniture table. There's a 1:many relationship linked with foreign keys: Area:House, House:Furniture
I would like to have a UI item with Areas populate a House UI item which in turn populates a Furniture UI item. By UI item, I mean something like an editable list, or series of lineEdits/Spinners (value type dependant), anything really.
just to clarify a bit, i understand about how to use qsqlrelationaltablemodel and qsqlrelationdelegate. These work fine when I'm updating a single table, but won't update a (sql) view created from joins on multiple tables.
I'm after a solution that lets me efficiently work on data from multiple tables, including updating the values.
Should I do this with three models, updating the filter on each, and using a view linked to each model?
Or perhaps I could insert everything into a single tree model?
Or should I be looking at creating a custom model, or perhaps just not using model/view and manually populating UI?
I have tried using multiple models on the same database as an experiment a while ago, and they didn't behave together as nicely as I'd hoped. This is the main reason I'm asking about best practice.
Much appreciation for any advice you can offer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我的问题并没有想象的那么清楚;也就是说,因为我真的不知道该问什么问题。
我正在寻找一种方法来通过单独的 UI 项目表示来自多个表的数据,这种方式不一定直接映射到表视图。
到目前为止,我的解决方案是使用单独的模型,手动从数据库填充。我需要手动对值进行任何编辑。
到目前为止似乎工作正常。
我学到的一个方便的技巧是将额外的数据放入不同的角色 Qt::UserRole 中。我可以将相关的 UUID(我的数据库键都是 UUID)与单列树视图中的数据一起存储,并使用它们来获得相当直接的数据库访问。额外的数据显然可以放入 Qt::UserRole+n 中,尽管我还没有尝试过。
I guess my question wasn't as clear as it could be; namely because I didn't really know the right question to ask.
I was looking for a way to represent data from multiple tables over separate UI items, in a way that didn't necessarily map directly to a tableview.
My solution so far is to use separate models, populated from the database manually. Any editing of the values that is required I am doing manually.
Seems to work ok so far.
One handy tip I've picked up is putting extra data in a different role Qt::UserRole. This was I can store pertinent UUIDS (my db keys are all UUIDs) with data in a single column treeview, and use these to get fairly direct db access. Extra data can apparently be put in Qt::UserRole+n, although I haven't tried that yet.