gpt4 book ai didi

c - 信号 'show' 不是从使用 GtkBuilder 构建的 GUI 发出的?

转载 作者:太空宇宙 更新时间:2023-11-03 23:40:47 24 4
gpt4 key购买 nike

我正在尝试通过“显示”信号的信号处理程序从 GSettings 初始化 GtkEntry 小部件。这在手动构建用户界面时按预期工作,但在使用 GtkBuilder 时无法工作。这同样适用于其他 GtkWidget 信号; GtkEntry 信号按预期工作。看起来信号从未发出过?

#include <gtk/gtk.h>


void activated (GtkEntry *entry,
gpointer user_data){

GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(user_data), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
gtk_entry_get_text(entry));
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);

}



void shown (GtkWidget *widget,
gpointer user_data){
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(user_data), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE,
"SHOWN!");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);

}



int main(int argc, char **argv){
GtkWidget *window1, *window2, *entry;
GtkBuilder *builder;


gtk_init(&argc, &argv);

// Construct working window 1

window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window1),"Working");
entry=gtk_entry_new();

g_signal_connect(entry, "activate", G_CALLBACK(activated), window1);
g_signal_connect(entry, "show", G_CALLBACK(shown), window1);

gtk_container_add(GTK_WINDOW(window1), entry);


// Construct not working window 2

builder=gtk_builder_new_from_file ("window.glade");
window2 = GTK_WIDGET(gtk_builder_get_object(builder, "window2"));
gtk_builder_connect_signals(builder, NULL);
g_object_unref(builder);


gtk_widget_show_all(window1);
gtk_widget_show_all(window2);


gtk_main();

return 0;
}

window.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window2">
<property name="can_focus">False</property>
<property name="title" translatable="yes">Not working</property>
<child>
<object class="GtkEntry" id="entry1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="shadow_type">none</property>
<signal name="activate" handler="activated" object="window2" swapped="no"/>
<signal name="show" handler="shown" object="window2" swapped="no"/>
</object>
</child>
</object>
</interface>

最佳答案

GLADE中设置可见标志设置为 false(取消勾选),以便当 GtkBuilder 实例化 glade 文件内容时,不会显示小部件;然后当您为 window2 执行 gtk_widget_show_all 时,信号将被发射。

enter image description here

或者,编辑 window.glade 并将可见属性设置为 False 或将其删除(我认为默认情况下小部件不可见),例如:

 ...
<object class="GtkEntry" id="entry1">
<property name="visible">False</property>
...

关于c - 信号 'show' 不是从使用 GtkBuilder 构建的 GUI 发出的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46773255/

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