gpt4 book ai didi

C - GTKUibuilder - Glade - g_signal_connect 错误

转载 作者:太空宇宙 更新时间:2023-11-04 02:29:29 25 4
gpt4 key购买 nike

我正在尝试在我的项目中使用 Glade 来生成一个包含窗口、网格的文件……但是当我尝试监听单击我的按钮的事件时,我遇到了一个错误,我不明白为什么 :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 来检索它们而不是标签上的文本。

关于C - GTKUibuilder - Glade - g_signal_connect 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45673199/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com