C - GTKUibuilder - 林间空地 - g_signal_connect错误
我想在我的项目中使用Glade生成一个包含窗口,网格的文件......但是当我尝试听我点击按钮上的事件时,我不明白为什么:■C - GTKUibuilder - 林间空地 - g_signal_connect错误
我得到了这些错误..
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;
}
是有人有任何想改正它?
我用我的Ubuntu机器用Gtk 3.18试过了。
当我打开你的文件格莱德我不明白在UI的任何消息,但我得到的shell错误消息:
(空地: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检索它们而不是标签上的文本。
谢谢!这是我的案例的答案的90%, 事实上,我必须指出我的UI文件的确切路径,它不适用于相对路径:s – nodeover
我只是试图按照此示例:https://developer.gnome.org/gtk3/stable/ch01s03.html &得到了完全相同的错误...信号不希望听按钮:■ – nodeover
根据错误信息提供'NULL'指针。显然,连接信号时没有问题,而是从GUI中检索对象。你可能会检查你的返回值为'NULL'poiners。 – Gerhardh
您可以检查'gtk_builder_add_from_file'的返回值作为开始。建设者是否甚至可以找到你的UI文件? – Gerhardh