代码之家  ›  专栏  ›  技术社区  ›  nodeover

C-GTKUibuilder-Glade-g_signal_连接错误

  •  1
  • nodeover  · 技术社区  · 8 年前

    我犯了这些错误。。

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

    <?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>
    

    我的主要。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;
    }
    

    有人有办法纠正吗?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Gerhardh    8 年前

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

    (glade:2996):GladeUI警告**:文件在“object”标记下不包含所需的属性“id”。

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

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

    您只有:

    <object class="GtkButton">
    

    没有ID,gtk_生成器不知道如何找到此小部件。

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

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

    button = gtk_builder_get_object (builder, "button1");
    

    在这里,ID用于检索指向小部件的指针。