我只是在文本输入窗口小部件中按 Enter 时,尝试更改从文本内容窗口小部件中取出的标签内的文本。我正在使用Glade来设计这个简单的界面,只包含窗口内的那两个小部件。然后使用glib-compile-resources test.xml
将其转换为GIO资源并加载到程序中。
但标签不会改变。知道我做错了什么吗?编译器不会发出任何警告。我也可以打印标签的内容,这是我从Glade设置的。
test.c的
#include <gtk/gtk.h>
static void on_entry_activate(GtkWidget *entry, gpointer data) {
const gchar *text = gtk_entry_get_text(GTK_ENTRY(entry));
GtkBuilder *app_builder = gtk_builder_new_from_resource(
"/org/samik/test/test.glade");
GObject *label = gtk_builder_get_object(app_builder, "label1");
g_print("%s\n", gtk_label_get_text(GTK_LABEL(label)));
gtk_label_set_text(GTK_LABEL(label), text);
}
static void on_app_activate(GApplication *app, gpointer data) {
GError *res_load_err = NULL;
GResource *app_res = g_resource_load("test.gresource", &res_load_err);
g_resources_register(app_res);
GtkBuilder *app_builder = gtk_builder_new_from_resource(
"/org/samik/test/test.glade");
GObject *entry = gtk_builder_get_object(app_builder, "entry1");
GObject *main_window = gtk_builder_get_object(app_builder, "window1");
g_signal_connect(GTK_WIDGET(entry), "activate",
G_CALLBACK(on_entry_activate), NULL);
gtk_window_set_application(GTK_WINDOW(main_window), GTK_APPLICATION(app));
gtk_widget_show_all(GTK_WIDGET(main_window));
}
int main(int argc, char *argv[]) {
int status;
GtkApplication *app = gtk_application_new("org.samik.draw",
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(on_app_activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}
test.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
<requires lib="gtk+" version="3.10"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="title" translatable="yes">test</property>
<child>
<object class="GtkFixed" id="fixed1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<!-- take whatever user types in this text-entry -->
<object class="GtkEntry" id="entry1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="x">197</property>
<property name="y">119</property>
</packing>
</child>
<child>
<!-- and put it here -->
<object class="GtkLabel" id="label1">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">label</property>
</object>
<packing>
<property name="x">53</property>
<property name="y">56</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
的test.xml
<?xml version="1.0" encoding="UTF-8"?>
<gresources>
<gresource prefix="/org/samik/test">
<file compressed="true">test.glade</file>
</gresource>
</gresources>
答案 0 :(得分:0)
当您拨打gtk_builder_new_from_resource()
两次时,您实际上是在创建两个窗口(但只显示一个窗口)。修改标签的调用是正确的,但它会修改不可见窗口中的标签。
在初始化时仅加载一次资源,并为回调函数提供指向实际窗口小部件的指针。通常,您将创建一个结构,其中包含指向稍后将要使用的所有小部件的指针,然后将指向该结构的指针作为userdata传递给回调函数,如on_entry_activate()
。