我的 UI 元素和模型对象应该有什么级别的分离?

发布于 2024-10-10 16:27:02 字数 355 浏览 0 评论 0原文

我正在 QT 中构建一个桌面应用程序(尽管这可能无关紧要),并且我很难完成类结构和布局。数据模型相当简单,有一个根容器和多个项目容器。考虑以下表示:

Root
+ Parent
  + Child

UI 相当简单,遵循该数据模型,主窗口带有可滚动区域(根),小部件包含自定义小部件(子部件)的布局(父部件)以及一些标签和按钮。

我的麻烦在于处理需要沿着链向上然后返回的事件,例如将子项从一个父项移动到另一个父项、移动元素或更新影响多个或许多其他小部件的子元数据。

我目前正在拆分 UI 小部件和模型对象,但让每个小部件和相应的模型对象相互指向和相互指向感觉很麻烦,而且我认为这会导致过多的维护。

I'm building a desktop app in QT (although that may be irrelevant) and I'm having a hard time working through the class structure and layout. The data model is fairly simple with a root container with a number of containers of items. Consider the following representation:

Root
+ Parent
  + Child

The UI is fairly simple and follows that data model with a main window with a scrollable area (root), widgets that contain layouts (parents) of custom widgets (children) with some labels and buttons.

My trouble is with handling events that need to go up the chain and then back down like moving a child from one parent to another, moving elements, or updating child meta-data that impacts several to many other widgets.

I'm currently splitting UI widgets and model objects but having each widget and corresponding model object pointing to and from each other feels cumbersome and I think it is leading to too much maintenance.

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

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

发布评论

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

评论(2

世界和平 2024-10-17 16:27:02

我建议遵循标准的 MVC 模式并确保模型到视图不存在依赖关系;在您的情况下,这意味着虽然每个模型项都有一个小部件,但模型项不会引用其相应的小部件。

虽然 MVC 模式有多种风格,但实现这一点的一种方法是使用一个视图类来监视模型的任何更改并相应地更新视图(这可以通过将视图类中的槽连接到从模型类)。用户通过视图发起的任何更改都可以:

1)直接由模型处理
通过一个简单的信号/槽
连接
2)处理
通过控制器类可以
指示模型相应更新,

其中任何一个都会导致模型发出更新信号,从而导致视图更新。这样做的好处是能够更改视图(或添加其他视图),而无需更新模型。

我建议阅读 Qt 的模型/视图编程指南更好地理解 MVC 在 Qt 中的工作原理,并查看是否存在现有的类或接口(例如 QAbstractItemModel),您可以使用它来代替自己烘焙。

I'd suggest following a standard MVC pattern and ensure there are no dependencies from the model to the view; in your case this would mean that while there is a widget for every model item, the model items do not reference their corresponding widgets.

While the MVC pattern has many flavours, one way to accomplish this would be to have a view class that monitors the model for any changes and updates the view accordingly (this can be accomplished by connecting a slot in the view class to a signal emitted from the model class). Any changes the user initiates through the view can then be:

1) handled directly by the model
through a simple signal/slot
connection
2) handled
by a controller class which can
direct the model to update accordingly

Either of these would then cause the model to emit an update signal which would cause your view to update. The benefit of this is the ability to change your view (or add additional views) without having to update your model.

I'd recommend reading Qt's Model/View Programming Guide to better understand how MVC works in Qt and to see if there's an existing class or interface (e.g. QAbstractItemModel) that you could use instead of baking your own.

想念有你 2024-10-17 16:27:02

考虑使用工厂模式和命令模式。有很多样品。我在这里只是给出一个提示。

http://en.wikipedia.org/wiki/Factory_method_pattern

http://en.wikipedia.org/wiki/Command_pattern

忘记提及 qt 书:cartan-cas-dot-suffolk-dot-教育/oopdocbook/html/

Consider using factory pattern and command pattern. There are plenty of samples. I am just giving a hint here.

http://en.wikipedia.org/wiki/Factory_method_pattern

http://en.wikipedia.org/wiki/Command_pattern

Forgot mention about qt book: cartan-cas-dot-suffolk-dot-edu/oopdocbook/html/

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