C++动态显示或隐藏 FLTK 小部件?

发布于 2024-10-28 06:01:40 字数 719 浏览 5 评论 0原文

我是 C++ 新手,以前从未使用过宏,但我相信它们可能允许我解决以下问题,尽管我欢迎其他解决方案。

我编写了以下宏:

#define COMMAND(NUMBER){ button_ ## NUMBER ## ->hide(); }

我想这样调用它

for (int i = 1; i < 10; i++)
{
    COMMAND(i)
}

,希望当程序执行时它会执行相当于:

button_1->hide();
button_2->hide();
button_3->hide();
button_4->hide();
       .
       .
button_10->hide();

这个想法是我想根据配置动态地使 FLTK GUI 显示/隐藏小部件加载表单时读取的文件。

不幸的是,上面的宏似乎不起作用,它所做的是

button_i->hide();

导致编译错误,因为button_i不存在!

所以我的问题是:
A) 在 C++ 中可以做到这一点吗?
B) 宏能够完成这个任务吗?
C) 如果不能的话什么可以?
D)如果宏可以做到这一点,那么我该如何修改上面的代码才能真正让它工作!

谢谢

I'm new to C++ and never used macros before but I beleive they may allow me to solve the following problem although I would welcome alternative solutions.

I have written the following macro:

#define COMMAND(NUMBER){ button_ ## NUMBER ## ->hide(); }

and I want to call it like this

for (int i = 1; i < 10; i++)
{
    COMMAND(i)
}

in the hope that when the program executes it would do the equivalent of:

button_1->hide();
button_2->hide();
button_3->hide();
button_4->hide();
       .
       .
button_10->hide();

The idea is that I want to make a FLTK GUI display/hide widgets dynamically depending on a configuration file being read in when the form is loaded.

Unfortunately it appears that the macro above doesn't work and instead what it does is

button_i->hide();

which causes a compile error because button_i doesn't exist!

So my questions are:
A) Is it possible to do this in C++?
B) Is a macro able to accomplish this?
C) If not what can?
D) If macros can do this then how do I amend the above code to actually get it to work!

Thanks

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

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

发布评论

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

评论(3

黯淡〆 2024-11-04 06:01:40

我很确定有一种方法可以使宏技巧发挥作用,但还有更好的选择。

例如,您可以将按钮存储在数组中。

假设您的按钮有一个名为 struct button 的类型,请像这样声明您的按钮:

struct button *buttons[42];

您需要像初始化各个 Button_X 变量一样初始化所有按钮。

button[i] = <whatever you do to create button number i>;

然后您可以简单地执行以下操作:

for (int i=0; i<42; i++) {
  button[i]->hide();
}

或仅使用其编号切换任何单独的按钮。从长远来看,您可能会发现这比任何宏技巧都要好。

(警告:数组是基于 0 的,因此您的第一个按钮是 button[0]

I'm pretty sure there is a way to make a macro trick work, but there are better options.

You can, for example, store your buttons in an array.

Assuming your buttons have a type called struct button, declare your buttons like this:

struct button *buttons[42];

You need to initialize all of them as you would have for your individual button_X variables.

button[i] = <whatever you do to create button number i>;

Then you can simply do:

for (int i=0; i<42; i++) {
  button[i]->hide();
}

Or toggle any individual button with just its number. You'll probably find that better than any macro trick in the long run.

(Warning: arrays are 0-based, so your first button is button[0])

守望孤独 2024-11-04 06:01:40

如果您可以控制 button_2 等变量,我建议您使用数组。这样你就可以使用如下所示的内容:

for (int i = 1; i < 10; i++)
{
  button[i]->hide();
}

当然,这对于放置在宏中来说是微不足道的。

一般来说,您无法使用 C 语言中的普通循环来完成您想要的任务。但是,如果您确实想要走这条路,请查看 boost 预处理器包。

If you have control over the button_2 etc. variables, I would recommend using an array instead. That way you could use a look like:

for (int i = 1; i < 10; i++)
{
  button[i]->hide();
}

Of course, this is trivial to place in a macro.

In general, you can't accomplish what you want with a plain loop in C. However, if you really want to go that route, look at the boost preprocessor package.

客…行舟 2024-11-04 06:01:40

不要尝试自己管理按钮,FLTK 不喜欢这样。您将拥有一系列看不到的很棒的按钮。我不确定如何按照您想要的方式执行此操作,但我会这样做:

Fl_Button * o = (Fl_Button *)myWindow->child(i);
o->hide();

并让按钮成为 FLTK 窗口的子级。

Do not try to manage the buttons yourself, FLTK does not like that. You will have an awesome array of buttons that you cannot see. I am not sure how to do it the way you want but I would do it this way:

Fl_Button * o = (Fl_Button *)myWindow->child(i);
o->hide();

and let the buttons be children of the FLTK window.

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