如何在gtkmm3中动态命名小部件变量?

时间:2018-12-31 12:10:45

标签: c++ string widget instantiation 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); //...

  

Qt但类似的问题是here

我已经尝试过Qt example,但是它具有文件名,我不知道它是用于。

对于字符串数组(带有“ button1”,“ button2”,“ button3”的列表)循环将在此处生成指针错误

grid.add(*button1)
         ^_______

我需要做的是为Buttons的变量生成动态命名,然后执行

grid.add(*button1);
grid.add(*button2);
grid.add(*button3);
...
grid.add(*button4);

1 个答案:

答案 0 :(得分:1)

试图弄清楚我的问题,我最终从各个人那里收集信息,并将其组合到以下内容中

  

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);
}

感谢ptomatoPeter87