简体   繁体   中英

gtkmm: how to create popover menu without builder?

I am trying to make GTK3 application with C++. Because it is my first gtkmm app and it is really small, I am avoiding builder and placing widgets with plain code.

I have such snippet for titlebar's menu button:

Gtk::MenuButton mbtn;
Gtk::Menu menu;
Gtk::MenuItem mnitSettings {"Settings"};
Gtk::MenuItem mnitAbout {"About"};
mbtn.set_image_from_icon_name("open-menu-symbolic");
menu.append(mnitSettings);
menu.append(mnitAbout);
menu.show_all();
mbtn.set_popup(menu);

It works fine, but I noticed that most GTK3 applications have some kind of Gtk::Popover for button's menu, which have transition animation and pointing arrow on it's edge. For my sadness, most GTK3 applications use builder, so I can not understand how to do the trick.

There is Gtk::MenuButton::set_popover(Gtk::Popover &), but I failed to add my menu to popover wrapper (I've got "Attempting to add a widget with type gtkmm__GtkMenu to a container of type gtkmm__GtkPopover, but the widget is already inside a container of type GtkWindow" warning).

How could popover menu be achieved in this case?

I just figured out how to.

Gtk::MenuButton mbtn;
Glib::RefPtr<Gio::Menu> menu = Gio::Menu::create();
menu->append("Settings", "app.settings");
menu->append("About", "app.about");
mbtn.set_menu_model(menu);

Actions can be attached with:

app->add_action("settings", glibc::ptr_fun(&some_useful_func));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM