在查看使用Gtk和其他库用C编写的gnome-builder代码时,我发现了用于快捷窗口定义的以下代码:
ide-快捷键-窗口-私人。h:
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define IDE_TYPE_SHORTCUTS_WINDOW (ide_shortcuts_window_get_type())
G_DECLARE_FINAL_TYPE (IdeShortcutsWindow, ide_shortcuts_window, IDE, SHORTCUTS_WINDOW, GtkShortcutsWindow)
G_END_DECLS快捷键-窗口c:
#define G_LOG_DOMAIN "ide-shortcuts-window"
#include "config.h"
#include <glib/gi18n.h>
#include "ide-shortcuts-window-private.h"
struct _IdeShortcutsWindow
{
GtkShortcutsWindow parent_instance;
};
G_DEFINE_TYPE (IdeShortcutsWindow, ide_shortcuts_window, GTK_TYPE_SHORTCUTS_WINDOW)
static void
ide_shortcuts_window_class_init (IdeShortcutsWindowClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/libide-gui/ui/ide-shortcuts-window.ui");
}
static void
ide_shortcuts_window_init (IdeShortcutsWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}这个ui文件描述了窗口:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<!-- interface-requires gtk+ 3.19 -->
<template class="IdeShortcutsWindow">
<property name="modal">true</property>
<child>
<object class="GtkShortcutsSection">
<property name="visible">true</property>
<property name="section-name">editor</property>
<property name="title" translatable="yes" context="shortcut window">Editor Shortcuts</property>
<child>
<object class="GtkShortcutsGroup">
<property name="visible">true</property>
<property name="title" translatable="yes" context="shortcut window">General</property>
<child>
<object class="GtkShortcutsShortcut">
<property name="visible">true</property>
<property name="title" translatable="yes" context="shortcut window">Show help</property>
<property name="accelerator">F1</property>
</object>
</child>
............... 我以前构建过一些Gtk应用程序,但我从未见过或使用过这样的描述对象的方法,而且我很难理解这段代码。我知道.ui文件描述了一个模板,从模板中将初始化结构IdeShortcutsWindow。但我不太懂怎么用它。是否应该创建这样的结构实例:
IdeShortcutsWindow shortcuts;但是我如何初始化它呢?对于实例,我应该调用ide-快捷方式-window.c中定义的函数吗?在查看其余代码时,我没有找到使用IdeShortcutsWindow实例的任何其他地方。
发布于 2021-02-12 04:11:07
我是否应该创建类似于这样的结构实例:
IdeShortcutsWindow shortcuts;
类的一个新实例是用g_object_new (IDE_TYPE_SHORTCUTS_WINDOW, NULL)创建的。您可以在gnome构建器代码中看到同样的情况。
有时还提供了帮助函数来创建新实例。例如:gtk_button_new ()和g_object_new (GTK_TYPE_BUTTON, NULL)是同样的。这适用于所有GObject,而不是特定于GtkBuilder模板。
请注意,some_type_new()是一个只能在C语言中使用的函数。使用GObject内省绑定的语言(例如:g_object_new (...) )只能使用g_object_new (...)来创建新对象。
就我个人而言,我维护一个模板项目来帮助我轻松地开发GTK应用程序,可能会对您有所帮助:链接
https://stackoverflow.com/questions/66121606
复制相似问题