GtkApplication 的初始化 - 我应该使用 GObject 的 "init" 还是 GtkApplication 的 "startup"?

Initialization of GtkApplication - Should I use GObject's "init" or GtkApplication's "startup"?

我正在尝试创建一个 GTK-3 应用程序,在初始化过程中我想检查是否设置了 GSetting 的值,否则我想显示一个 "startup" 对话框,该对话框将仅在第一个 运行 可见。

GObject 有一个 _init 后缀,可用于 class 初始化。在这种情况下,MyGtkApplication 将被构建,它基于 GtkApplicationGtkApplicationstartup-东西也可以用于初始化。

在那个例子中,我仍然需要为 GApplicationClass 实现 _init 函数。

GApplicationClass *gapp_class;

gapp_class = G_APPLICATION_CLASS (class);
gapp_class->startup = app_startup;

// This must be always implemented, because the MyGtkApplication is "GtkApplication"-based class.
void app_init(MyGtkApplication *app) {
  // Check and show the modal dialog if key does not exist?
}

// This will overwrite the GApplicatio's "startup".
void app_startup(GApplication *app) {
  // Check and show the modal dialog if key does not exist?
}

目前app_init没有尸体。

有什么区别,我应该使用哪一个?

此致, TheAifam5.

init 基本上是 GApplication 对象的构造函数。用它来初始化对象的私有数据并使其进入一致状态。

startup 在应用程序启动时被调用。特别是,在您调用 g_application_run() 并启动主事件循环后,应用程序已检查它是唯一的实例 运行。您不想在此之前显示对话框,因此 startup 是正确的地方。