我如何在 gtkmm3 中动态命名小部件变量?
How can I dynamically name widget variables in gtkmm3?
我正在尝试创建 10 个 Gtk::Buttons 而不是
Gtk::Button *button1 = new Gtk::Button;
Gtk::Button *button2 = new Gtk::Button;
Gtk::Button *button3 = new Gtk::Button;
...
Gtk::Button *button10 = new Gtk::Button;
grid.add(*button1); grid.add(*button2); grid.add(*button3); //...
A Qt but similar question is here
我试过 Qt example,但它有文件名,我不知道它是为了什么。
对于字符串数组(有"button1","button2","button3"的列表)循环会在这里产生指针错误
grid.add(*button1)
^_______
我需要做的是为按钮的变量生成动态命名,然后执行
grid.add(*button1);
grid.add(*button2);
grid.add(*button3);
...
grid.add(*button4);
为了弄清楚我的问题,我最终从不同的人那里挑选了信息并将其组合成以下内容
main.cpp
#include <gtkmm.h>
#include <map>
#include <string>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.dynamic.buttons");
Gtk::Window window;Gtk::Grid grid;
window.set_default_size(600, 600);
std::string name = "button";
std::map<std::string, Gtk::Button*> all_buttons;
for (int i = 1; i <= 10; i++)
{
Gtk::Button *button = new Gtk::Button("Button " + std::to_string(i));
all_buttons[name] = button;
grid.add(*button);
}
window.add(grid);
window.show_all_children();
return app->run(window);
}
我正在尝试创建 10 个 Gtk::Buttons 而不是
Gtk::Button *button1 = new Gtk::Button;
Gtk::Button *button2 = new Gtk::Button;
Gtk::Button *button3 = new Gtk::Button;
...
Gtk::Button *button10 = new Gtk::Button;
grid.add(*button1); grid.add(*button2); grid.add(*button3); //...
A Qt but similar question is here
我试过 Qt example,但它有文件名,我不知道它是为了什么。
对于字符串数组(有"button1","button2","button3"的列表)循环会在这里产生指针错误
grid.add(*button1)
^_______
我需要做的是为按钮的变量生成动态命名,然后执行
grid.add(*button1);
grid.add(*button2);
grid.add(*button3);
...
grid.add(*button4);
为了弄清楚我的问题,我最终从不同的人那里挑选了信息并将其组合成以下内容
main.cpp
#include <gtkmm.h>
#include <map>
#include <string>
int main(int argc, char *argv[])
{
auto app =
Gtk::Application::create(argc, argv,
"org.gtkmm.dynamic.buttons");
Gtk::Window window;Gtk::Grid grid;
window.set_default_size(600, 600);
std::string name = "button";
std::map<std::string, Gtk::Button*> all_buttons;
for (int i = 1; i <= 10; i++)
{
Gtk::Button *button = new Gtk::Button("Button " + std::to_string(i));
all_buttons[name] = button;
grid.add(*button);
}
window.add(grid);
window.show_all_children();
return app->run(window);
}