首页 > 解决方案 > 如何使用菜单按钮 (Gtkmm)

问题描述

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

#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()也不起作用。结果: 在此处输入图像描述

标签: gtkgtkmm

解决方案


嗯,没错!您只需要致电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);
}

推荐阅读