我正在尝试在我的项目中使用Glade生成一个包含windows,grid的文件......但是当我尝试听到点击我的按钮的事件时,我遇到了错误,我不明白为什么:s < / p>
我收到了这些错误..
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;
}
有人有任何想法纠正它吗?
答案 0 :(得分:2)
我用我的Ubuntu机器用Gtk 3.18试了一下。
当我在Glade中加载你的文件时,我在UI中没有收到任何消息,但我在shell上收到错误消息:
(glade:2996):GladeUI-WARNING **:文件不包含必需的属性“id”在“object”标签下。
这是一个很好的暗示。 如果你查看你链接的例子,你会发现:
<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来检索它们,而不是标签上的文本。