C - GTKUibuilder - Glade - g_signal_connect 错误

C - GTKUibuilder - Glade - g_signal_connect error

我正在尝试在我的项目中使用 Glade 来生成一个包含 windows、网格的文件...但是当我尝试收听单击我的按钮的事件时,我遇到了一个错误,我确实这样做了不明白为什么 :s

我遇到了这些错误..

 GLib-GObject-WARNING **: invalid (NULL) pointer instance

(test:5608): GLib-GObject-CRITICAL **: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed

...对于每个听众。

所以我的文件 "project_ui.ui" 设置在与 main.c 相同的目录中:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Button 1</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Button 2</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">2</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Button 0</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkButton">
            <property name="label" translatable="yes">Quit</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">3</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

我的 main.c 包含此代码:

#include <stdio.h>
#include <gtk-3.0/gtk/gtk.h>


static void printHello (GtkWidget *widget, gpointer data){
  g_print("Hello World \n");
}

int main(int argc, char *argv[]) {
  GtkBuilder *builder;
  GObject *window;
  GObject *button;

  gtk_init(&argc,&argv);

  builder = gtk_builder_new();
  gtk_builder_add_from_file(builder, "project_ui.ui", NULL);

  window = gtk_builder_get_object(builder, "window");
  g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

  //Button 1
  button = gtk_builder_get_object(builder, "Button 1");
  g_signal_connect(button, "clicked", G_CALLBACK(printHello), NULL);

  //Button 2
  button = gtk_builder_get_object(builder, "Button 2");
  g_signal_connect(button, "clicked", G_CALLBACK(printHello), NULL);

  //Button 0
  button = gtk_builder_get_object(builder, "Button 0");
  g_signal_connect(button, "clicked", G_CALLBACK(printHello), NULL);

  //button Quit
  button = gtk_builder_get_object(builder, "Quit");
  g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), window);

  gtk_main();
  return 0;
}

有人有办法改正吗?

我在装有 Gtk 3.18 的 Ubuntu 机器上试过了。

当我在 Glade 中加载您的文件时,我在 UI 中没有收到任何消息,但在 shell:

中收到错误消息

(glade:2996): GladeUI-WARNING **: The file did not contain the required property "id" Under the "object" tag.

这是一个很好的提示。 如果您查看链接的示例,您会发现:

<object id="button2" class="GtkButton">

你只有这个:

<object class="GtkButton">

没有 ID,gtk_builder 不知道如何找到这个小部件。

因为您在搜索小部件时使用了错误的值:

button = gtk_builder_get_object(builder, "Button 1");

这只是按钮上显示的文本。您需要使用的是缺少的 id。 该示例使用此代码:

button = gtk_builder_get_object (builder, "button1");

此处的 ID 用于检索指向小部件的指针。

要解决您的问题,您需要向小部件添加 ID,并使用 ID 代替标签上的文本来检索它们。