如何将 Qt 模型/视图框架与图形视图框架结合使用

发布于 2024-09-08 06:05:38 字数 860 浏览 3 评论 0原文

我正在开发一个地图应用程序,需要使用表格、表单以及地图中的图形对象来显示数据对象。我正在使用 PyQt,但这并不重要,因为这是 Qt 问题而不是 Python 问题。

如果我只需要表和表单视图,这很容易,我只需使用 Qt 模型/视图框架。但是,我需要地图视图来提供仅使用图形视图框架才真正可用的功能,该框架本质上是它自己的模型/视图框架,其中 QGraphicsScene 充当数据模型。

我可以想到两种方法来做到这一点。一种方法是从 QAbstractItemModel 子类的权威模型开始,将其链接到 QAbstractItemView 的子类,并从那里生成和更新场景中的 QGraphicsItems。这看起来很难看,因为我不确定如何通过与 QGraphicsItems 交互来处理用户与数据项的交互和更改。

我可以想到的另一种方法是将 QGraphicsScene 视为权威数据源,将数据对象存储在每个 QGraphicsItem 的 .data() 属性中。然后,我将 QAbstractItemModel 子类化并编写它,以便它作为数据存储访问场景中的数据,然后其他视图将使用它作为它们的模型。我如何将对场景中数据的更改传播到模型?

无论我采用哪种方法,框架似乎都无法处理差距。在模型/视图中,假定所有更改都是在模型中进行的。在图形视图中,假定所有更改都是在场景中进行的。

那么您会选择哪种方法 QAbstractItemModel(authoritative)->QAbstractItemView->QGraphicsScene 或者 QGraphicsScene(authoritative)->QAbstractItemModel->Other Views。您为什么会选择其中之一?您预计会遇到哪些问题?还有其他人需要弥合 Qt 的孪生模型/视图框架之间的差距吗?你们是如何做到的?

I am working on a mapping application and need to display the data objects using a table, a form and as graphical objects in the map. I'm using PyQt, but that's not really important as this is a Qt question not a Python question.

If I only needed the table and form views this would be easy, I'd just use the Qt Model/View framework. However I need the map view to provide functionality only really available using the Graphics View framework, which is essentially it's own Model/View framework with the QGraphicsScene acting as the data model.

I can think of two ways to do this. One would be to start with an authoritative model subclassed from QAbstractItemModel, link it to a subclass of QAbstractItemView and from there generate and update QGraphicsItems in the scene. This looks ugly though because I'm not sure how to handle user interaction with and changes to the data items though interaction with the QGraphicsItems.

The other way I can think to do it is to treat the QGraphicsScene as the authoritative data source, storing the data object in each QGraphicsItem's .data() property. I'd then subclass QAbstractItemModel and write it so that it accesses the data in the scene as it's data store, the other views would then use this as their model. How would I propagate changes to the data in the scene up to the model though?

Whichever approach I take, it looks like there's a gap not handled by the frameworks. In Model/View all changes are assumed to be made in the model. In Graphics View all changes are assumed to be made in the scene.

So which approach would you choose QAbstractItemModel(authoritative)->QAbstractItemView->QGraphicsScene or alternatively QGraphicsScene(authoritative)->QAbstractItemModel->Other Views. Why would you choose one over the other and what gotchas do you anticipate? Has anyone else needed to bridge this gap between Qt's twin model/view frameworks and how did you do it?

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

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

发布评论

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

评论(1

绝情姑娘 2024-09-15 06:05:38

QAbstractItemModel(权威)->QAbstractItemView->QGraphicsScene

毫无疑问。我以前做过这个,它确实需要一些重复(至少是一些我无法避免的重复),但也没什么太糟糕的。

这还允许您在标准视图中表示数据以及场景,这非常好。

我最好的建议是存储 QHash QPersistantModelIndexQGraphicsItemQGraphicsSceneQAbstractItemView 您创建的。这使您可以快速在模型/视图区域之间切换 (QModelIndex)到图形视图区域(QGraphicsItem )

QAbstractItemModel(authoritative)->QAbstractItemView->QGraphicsScene

Without a doubt. I have done this before, it does require a bit of duplication (at least some that I couldn't avoid) but nothing too bad.

This also allows you to represent your data in standard views along with the scene which is quite nice.

My best advice would be to store a QHash of QPersistantModelIndex to QGraphicsItem and a QGraphicsScene in the QAbstractItemView you create. This allows you to quickly go between Model/View land (QModelIndex) to Graphics View land (QGraphicsItem)

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