我想在我的項目中使用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;
}
是有人有任何想改正它?
我只是試圖按照此示例:https://developer.gnome.org/gtk3/stable/ch01s03.html &得到了完全相同的錯誤...信號不希望聽按鈕:■ – nodeover
根據錯誤信息提供'NULL'指針。顯然,連接信號時沒有問題,而是從GUI中檢索對象。你可能會檢查你的返回值爲'NULL'poiners。 – Gerhardh
您可以檢查'gtk_builder_add_from_file'的返回值作爲開始。建設者是否甚至可以找到你的UI文件? – Gerhardh