具有多个 GUI 屏幕的程序的设计方法
我对自己感到非常困惑,以至于无法回头,觉得我的项目膨胀得太大,无法跟上我目前的方式。
简而言之:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是一些建议:
例如:
所有屏幕都可以从包含指向屏幕管理器的指针的基类继承(它们通过构造函数获取该指针或从静态单例实例中获取)。这样,您的所有屏幕都能够请求新的屏幕导航。
Here are a few suggestions:
For instance:
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.
顺便说一句:我不完全确定 FLTK 的内存结构。隐藏屏幕可能根本不会删除内存,而只是隐藏窗口的 GUI 表示,以便您稍后在相同状态下再次打开它。
GUI 的一个好方法是模型-视图-控制器架构,您可以在其中使用控制器来根据需要操作 GUI。
这将更多地表现为:
或者类似的东西,让你的窗口有一个中央编排器。这允许:
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:
Or some such similar thing to have a central orchestrator of your windows. This allows for: