如何使用 MenuButton (Gtkmm)

How to use a MenuButton (Gtkmm)

我正在尝试使用 MenuButton 但它不起作用。

#include <gtkmm.h>

int main( int argc, char **argv)
{
   Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "App1" );
   Gtk::Window window;

   Gtk::MenuButton menuButton;
   menuButton.set_label("menu button");
   Gtk::Menu menu;
   Gtk::Label label1("label1");
   Gtk::Label label2("label2");
   Gtk::MenuItem item1(label1);
   Gtk::MenuItem item2(label2);
   menu.append(item1);
   menu.append(item2);
   menuButton.set_popup(menu);

   window.add(menuButton);
   window.show_all();
   return app->run(window);
}

它不起作用。调用 set_menu() 而不是 set_popup() 也不起作用。 结果:

嗯,没错!您只需要致电 menu.show_all():

#include <gtkmm.h>

int main( int argc, char **argv)
{
   Glib::RefPtr< Gtk::Application >  app = Gtk::Application::create( "App1" );
   Gtk::Window window;

   Gtk::MenuButton menuButton;
   menuButton.set_label("menu button");
   Gtk::Menu menu;
   Gtk::Label label1("label1");
   Gtk::Label label2("label2");
   Gtk::MenuItem item1(label1);
   Gtk::MenuItem item2(label2);
   menu.append(item1);
   menu.append(item2);
   menu.show_all();
   menuButton.set_popup(menu);

   window.add(menuButton);
   window.show_all();
   return app->run(window);
}