我试图重现GTK教程的“example-4.c”在这里描述:https://docs.gtk.org/gtk4/getting_started.html
一个简单的例子,使用XML描述的UI +一些c代码。而不是使用GTK 4,我使用GTK 3,所以,编译:
gcc `pkg-config --libs --cflags gtk+-3.0` exemple4.c -o hello `pkg-config --libs gtk+-3.0`
example4.c和builder.ui(类似xml的文件)都在同一个文件夹中。
主窗口弹出但为空,我得到以下错误(两次):
(hello:19840): GLib-GObject-WARNING **: 19:38:31.985: invalid (NULL) pointer instance
(hello:19840): GLib-GObject-CRITICAL **: 19:38:31.985: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
(hello:19840): GLib-GObject-WARNING **: 19:38:31.985: invalid (NULL) pointer instance
(hello:19840): GLib-GObject-CRITICAL **: 19:38:31.985: g_signal_connect_data: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
我猜这是简单的东西有关的两个按钮,但我错过了为什么。
下面是后两个文件的内容:example4.c:
#include <gtk-3.0/gtk/gtk.h>
#include <glib/gstdio.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
static void
quit_cb (GtkWindow *window)
{
gtk_window_close (window);
}
static void
activate (GtkApplication *app,
gpointer user_data)
{
/* Construct a GtkBuilder instance and load our UI description */
GtkBuilder *builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "builder.ui", NULL);
/* Connect signal handlers to the constructed widgets. */
GObject *window = gtk_builder_get_object (builder, "window");
gtk_window_set_application (GTK_WINDOW (window), app);
GObject *button = gtk_builder_get_object (builder, "button1");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
button = gtk_builder_get_object (builder, "button2");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
button = gtk_builder_get_object (builder, "quit");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (quit_cb), window);
gtk_widget_show (GTK_WIDGET (window));
/* We do not need the builder any more */
g_object_unref (builder);
}
int
main (int argc,
char *argv[])
{
#ifdef GTK_SRCDIR
g_chdir (GTK_SRCDIR);
#endif
GtkApplication *app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
int status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
和builder.ui文件:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<object id="window" class="GtkWindow">
<property name="title">Grid</property>
<child>
<object id="grid" class="GtkGrid">
<child>
<object id="button1" class="GtkButton">
<property name="label">Button 1</property>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object id="button2" class="GtkButton">
<property name="label">Button 2</property>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object id="quit" class="GtkButton">
<property name="label">Quit</property>
<layout>
<property name="column">0</property>
<property name="row">1</property>
<property name="column-span">2</property>
</layout>
</object>
</child>
</object>
</child>
</object>
</interface>
1条答案
按热度按时间lzfw57am1#
我发现了原因:builder.ui似乎坏了,我用glade创建了一个类似的UI,它没有错误。下面是我生成的新UI: