在 Glade / Gtkbuilder 中使用自定义小部件
我正在使用 Gtk 和 Glade 开发一个应用程序。我的印象是,为主窗口创建 GtkWindow 的子类是常见的做法,但我一直坚持如何从 GtkBuilder 定义构造我的子类。有人知道怎么做吗?
I'm developing an application with Gtk and Glade. My impression is that it's common practice to create a subclass of GtkWindow for your main window, but I'm stuck on how I would construct my subclass from a GtkBuilder definition. Does anyone know how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
子类化
GtkWindow
在 GTK 的各种语言绑定中比在纯 C 中更常见。您没有提到您正在使用哪种语言。也就是说,我在 C 中子类化
GtkWindow
的方式是在 Glade 中创建窗口的内容,而不是窗口本身。在 Glade 3 (IIRC) 中,您可以右键单击调色板中的小部件,然后选择“将小部件添加为顶级”以放置不带容器的非顶级小部件。然后为
GtkWindow
的子类编写代码,我们将其命名为MyAppWindow
。我不会在这个答案中讨论这个问题,因为 GObject 文档中有很多示例。在 init 函数 (my_app_window_init()
) 中加载 Glade 文件,使用gtk_builder_get_object()
获取指向 Glade 文件中最外层 widget 的指针,并使用gtk_container_add()
将其添加到您正在构建的窗口中。然后像平常一样使用gtk_builder_connect_signals()
。您必须以这种方式手动设置窗口的所有属性,因为您无法在 Glade 中执行此操作,但除此之外,我发现它效果很好。
Subclassing
GtkWindow
is more common in GTK's various language bindings than it is in plain C. You didn't mention which language you were using.That said, the way I subclass
GtkWindow
in C is to create the contents of the window in Glade, but not the window itself. In Glade 3 (IIRC) you can right-click on a widget in the palette and choose "Add widget as toplevel" to place a non-toplevel widget without a container.Then write code for your subclass of
GtkWindow
, let's call itMyAppWindow
. I won't go into that in this answer since there are plenty of examples in the GObject documentation. In the init function (my_app_window_init()
) load the Glade file, usegtk_builder_get_object()
to get a pointer to the outermost widget in the Glade file, and usegtk_container_add()
to add it to the window you are constructing. Then usegtk_builder_connect_signals()
as you normally would.You have to set all the window's properties manually this way, since you can't do it in Glade, but other than that I've found it works quite well.
子类化 GtkWindow 并不常见。
我认为不可能对从 gtkbuilder 定义创建的顶级窗口进行子类化。
gtkbuilder 在创建之前需要了解您的子类化小部件。
it is not common practice to subclass GtkWindow.
i don't think it is possible to subclass toplevel window created from gtkbuilder definition.
gtkbuilder needs to know about your subclassed widget before creation.
如果您确实想创建自己的 GtkWindow 子类,ptomato 很好地描述了基本步骤。还可以为空地创建插件,以使您的自定义小部件可用。但这并不容易,而且很可能不是您想要做的。
大多数应用程序仅使用标准小部件,而不对其进行子类化。然后使用 gtkbuilder(或 libglade)加载一个空地文件,您不需要为 GUI 提供一个特殊的类(就像在其他一些 RAD 工具中一样),而只需获得一组对象。 API 允许您按名称查找它们(窗口基本上只是其中之一)。一种常见的方法是在程序启动时查找要与之交互的所有小部件并将它们存储在全局变量中。或者,如果您需要窗口的多个实例,您可以创建一个结构来存储它们。或者您可以在每次需要时简单地查找小部件。请注意,您获得的对象集是完全动态的。例如,您可以在不同窗口之间移动小部件,就像以编程方式创建 GUI 一样。
If you really want to create your own subclass of GtkWindow ptomato describes the basic steps well. It is also possible to create plugins for glade to make your custom widgets available. But this is not very easy, and most likely not what you want to do.
Most applications only use standard widgets without subclassing any of them. Then loading a glade file with gtkbuilder (or libglade) you don't need to have a special class for your GUI (like in some other RAD tools) instead you just get a set of objects. The API lets you look them up by name (and the window is basically just one of them). A common approach is to look up all widgets you are going to interact with and store them in global variables when the program starts up. Or if you need several instances of the window you can create a struct to store them in. Or you can simple lookup the widgets each time you need them. Note that the set of objects you get is completely dynamic. You can for example move the widgets between different windows just as if you created the GUI programmatically.