具有多个 GUI 屏幕的程序的设计方法

发布于 2024-10-26 05:58:30 字数 707 浏览 6 评论 0原文

我对自己感到非常困惑,以至于无法回头,觉得我的项目膨胀得太大,无法跟上我目前的方式。

简而言之:

1)有许多图形屏幕(窗口),每个屏幕都是在其自己的 .cpp 中定义的类,并附带带有 public 和 public 的 .h 标头。私人减速。

2)我正在使用 FLTK GUI 工具包,因此当我离开屏幕时,我在其上调用“hide()”,我假设它会进行垃圾收集,然后我创建一个要跟随的任何屏幕的新实例。

我的问题是,如果一个屏幕(我们称之为屏幕 A)创建另一个屏幕(屏幕 B),那么我必须在屏幕 A 中包含屏幕 B 的头文件,并在屏幕 A 的 .cpp 中创建一个指向屏幕 B 的全局指针。

IE。屏幕 A 的伪代码

#include "screenb.h"

ScreenB* screenb_ptr; // global

...
Bunch of Code, constructors, deconstructors, etc
...

void ScreenA::exit_and_make_screen_b()
{
    ScreenA.hide();
    screenb_ptr = new ScreenB();
}

这是最好的方法吗?我觉得它很草率(还有内存泄漏?),我应该有一个类似虚拟 .cpp/.h 的东西来跟踪一堆外部限定的指针;特别是有时我必须后退/前进屏幕(即可以从多个其他屏幕跳回到主菜单屏幕)。任何建议表示赞赏!

I've terribly confused myself to the point of no return and feel my project is ballooning too huge to keep up with my current ways.

In a nutshell:

1) There are many graphical screens (windows), each screen is a class defined in its own .cpp with a accompanying .h header w/ public & private decelerations.

2) I'm using the FLTK GUI toolkit, so when I leave a screen I call "hide()" on it, which I assume does garbage collection, and then I create a new instance of whatever screen is to follow.

My issue is that if a screen (Screen A lets call it) creates another screen (Screen B), then I must include screen B's header file in Screen A, and I make a global pointer to Screen B in Screen A's .cpp.

ie. A pseuodocode for Screen A

#include "screenb.h"

ScreenB* screenb_ptr; // global

...
Bunch of Code, constructors, deconstructors, etc
...

void ScreenA::exit_and_make_screen_b()
{
    ScreenA.hide();
    screenb_ptr = new ScreenB();
}

Is this the best approach? I feel it's sloppy (and a memory leak?) and I should have something like a dummy .cpp/.h that keeps track of a bunch of extern-qualified pointers; especially as sometimes I must go back/forward screens (i.e. Can jump back to a Main Menu screen from a miltiple other screens). Any advice is appreciated!

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

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

发布评论

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

评论(2

向地狱狂奔 2024-11-02 05:58:30

以下是一些建议:

  1. 创建一个包含所有屏幕内容的新头文件。然后,您可以只包含这个头文件并捕获所有其他屏幕头文件。
  2. 您可能会考虑使用屏幕管理器来保留对屏幕的所有引用。然后,屏幕之间的导航将由屏幕管理员负责处理所有引用和指针。这样,您就不会将屏幕耦合在一起,而是通过一个共同的中介进行通信。

例如:

screenManager->NavigateScreen( SCREEN_USER_PROFILE );

所有屏幕都可以从包含指向屏幕管理器的指针的基类继承(它们通过构造函数获取该指针或从静态单例实例中获取)。这样,您的所有屏幕都能够请求新的屏幕导航。

Here are a few suggestions:

  1. Create a new header file with all of your screen includes. Then you can just include this one header file and capture all of your other screen header files.
  2. You might consider a screen manager that keeps all of the references to your screens. Navigation between screens is then left up to your screen manager who handles all references and pointers. This way you are not coupling screens together but they talk through a common mediator.

For instance:

screenManager->NavigateScreen( SCREEN_USER_PROFILE );

All of your screens can inherit from a base class that holds a pointer to the screen manager (which they take through their constructor or grab from a static singleton instance). This way all of your screens have the ability to request a new screen navigation.

不弃不离 2024-11-02 05:58:30

顺便说一句:我不完全确定 FLTK 的内存结构。隐藏屏幕可能根本不会删除内存,而只是隐藏窗口的 GUI 表示,以便您稍后在相同状态下再次打开它。

GUI 的一个好方法是模型-视图-控制器架构,您可以在其中使用控制器来根据需要操作 GUI。

这将更多地表现为:

WindowManager wm;

void ScreenA::exit()
{
        wm.registerExit(screenb_ptr);
        wm.actOnExit();
}

或者类似的东西,让你的窗口有一个中央编排器。这允许:

  • 可插拔接口
  • 更好的组织
  • 防止错误

BTW: I'm not entirely sure about FLTK's memory structure. Hiding the screen might not delete memory at all, but just hide the GUI representation of the window, allowing you to open it again later in the same state.

A good method for GUI is Model-View-Controller architecture, where you have a controller to operate the GUI as necessary.

This would manifest itself more as:

WindowManager wm;

void ScreenA::exit()
{
        wm.registerExit(screenb_ptr);
        wm.actOnExit();
}

Or some such similar thing to have a central orchestrator of your windows. This allows for:

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