简体   繁体   English

如何将 gtk4 PopoverMenu 附加到父窗口小部件?

[英]How to attach a gtk4 PopoverMenu to a parent widget?

This should be a no-brainer but I just can't seem to figure it out.这应该是一个明智的选择,但我似乎无法弄清楚。 If I build a Gtk.PopoverMenu:如果我构建一个 Gtk.PopoverMenu:

menu = Gio.Menu.new()
test = Gio.MenuItem.new(label='test', detailed_action='win.test')
menu.append_item(test)
popup = Gtk.PopoverMenu.new_from_model(menu)

I don't know how to attach it to the parent widget (in my case a ListBox item).我不知道如何将它附加到父小部件(在我的例子中是一个 ListBox 项目)。 The API docs say "Popovers are attached to a parent widget" but there seems to be no relevant method for attaching it to the parent. API 文档说“Popovers are attached to a parent widget”但是似乎没有相关的方法可以将它附加到父级。 Trying to popup.popup() results in a warning "Calling gtk_widget_realize() on a widget that isn't inside a toplevel window is not going to work very well. Widgets must be inside a toplevel container before realizing them", and a subsequent segfault.尝试popup.popup()会导致警告“在不在顶层 window 内的小部件上调用 gtk_widget_realize() 不会很好地工作。小部件必须在顶层容器内才能实现它们”,以及随后的段错误。

To set the parent of the PopoverMenu , just use its set_parent() method to set the parent to any widget:要设置PopoverMenu的父级,只需使用其set_parent()方法将父级设置为任何小部件:

...
popup = Gtk.PopoverMenu.new_from_model(menu)
popup.set_parent(parent_widget)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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