我无法弄清楚如何在我的C代码中为我在GLADE中创建的消息对话框编写信号处理程序。如果我没有使用GLADE,信号处理程序将包含构建消息对话框本身的必要信息。例如,“你确定要退出吗?”消息对话框的格式为:
void show_question(GtkWidget *widget, gpointer window) {
GtkWidget *dialog;
dialog = gtk_message_dialog_new(GTK_WINDOW(window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"Are you sure to quit?");
gtk_window_set_title(GTK_WINDOW(dialog), "Question");
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
}
同样,“关于窗口”将具有以下形式:
void show_about(GtkWidget *widget, gpointer data) {
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file("battery.png", NULL);
GtkWidget *dialog = gtk_about_dialog_new();
gtk_about_dialog_set_name(GTK_ABOUT_DIALOG(dialog), "Battery");
gtk_about_dialog_set_version(GTK_ABOUT_DIALOG(dialog), "0.9");
gtk_about_dialog_set_copyright(GTK_ABOUT_DIALOG(dialog), "(c) Jan Bodnar");
gtk_about_dialog_set_comments(GTK_ABOUT_DIALOG(dialog), "Battery is a simple tool for battery checking.");
gtk_about_dialog_set_website(GTK_ABOUT_DIALOG(dialog), "http://www.batteryhq.net");
gtk_about_dialog_set_logo(GTK_ABOUT_DIALOG(dialog), pixbuf);
g_object_unref(pixbuf), pixbuf = NULL;
gtk_dialog_run(GTK_DIALOG (dialog));
gtk_widget_destroy(dialog);
}
既然我在GLADE中完全创建了这些窗口,我不确定信号处理程序应采用什么形式(在C中)才能连接,比如点击帮助 - >关于在GLADE中创建的关于窗口,或连接点击文件 - >退出在GLADE中创建的“您确定要退出”消息对话框。我是GTK +和Glade的新手,我似乎无法找到解决这个问题的任何东西。任何帮助将不胜感激。
编辑:下面是一个示例glade XML文件,它由两个顶级窗口组成。第一个是主窗口,其中包含一个包含菜单栏的单个框。第二个是关于对话框。我正在寻找的是帮助编写信号处理程序,在单击文件后打开about窗口 - >关于菜单栏。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkMenuBar" id="menubar1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkMenuItem" id="menuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_File</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem1">
<property name="label">gtk-new</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem2">
<property name="label">gtk-open</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem3">
<property name="label">gtk-save</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem4">
<property name="label">gtk-save-as</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkSeparatorMenuItem" id="separatormenuitem1">
<property name="visible">True</property>
<property name="can_focus">False</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem5">
<property name="label">gtk-quit</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Edit</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem6">
<property name="label">gtk-cut</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem7">
<property name="label">gtk-copy</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem8">
<property name="label">gtk-paste</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem9">
<property name="label">gtk-delete</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_View</property>
<property name="use_underline">True</property>
</object>
</child>
<child>
<object class="GtkMenuItem" id="menuitem4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">_Help</property>
<property name="use_underline">True</property>
<child type="submenu">
<object class="GtkMenu" id="menu3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkImageMenuItem" id="imagemenuitem10">
<property name="label">gtk-about</property>
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_underline">True</property>
<property name="use_stock">True</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
</object>
</child>
</object>
<object class="GtkAboutDialog" id="window_about">
<property name="can_focus">False</property>
<property name="type_hint">dialog</property>
<property name="program_name">Glade</property>
<property name="logo_icon_name">image-missing</property>
<child internal-child="vbox">
<object class="GtkBox" id="aboutdialog-vbox1">
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<property name="spacing">2</property>
<child internal-child="action_area">
<object class="GtkButtonBox" id="aboutdialog-action_area1">
<property name="can_focus">False</property>
<property name="layout_style">end</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">0</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object>
</child>
</object>
</interface>
答案 0 :(得分:3)
如果理解正确,您需要通过glade创建对话框,而不是以编程方式创建对话框。
使用林间空地,您必须使用documentation。
让我们假设您创建了一个简单的Yes,No对话框( yesno_dialog.ui ):
#include <gtk/gtk.h>
void on_button_clicked (GtkButton *button, gpointer user_data) {
int response;
GtkWidget *dialog;
GtkBuilder *builder;
g_return_if_fail (user_data != NULL);
builder = gtk_builder_new_from_file("yesno_dialog.ui");
dialog = GTK_WIDGET(gtk_builder_get_object (builder, "dialog1"));
gtk_window_set_transient_for (GTK_WINDOW(dialog), GTK_WINDOW(user_data));
gtk_widget_show_all(dialog);
response = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
g_object_unref(G_OBJECT(builder));
g_print ("Response is %s\n", response == 1 ? "Yes" : "No");
}
int main(int argc, char *argv[]) {
GtkWidget *button;
GtkWidget *window;
gtk_init(&argc,&argv);
button = gtk_button_new_with_label("Press for dialog");
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_container_add(GTK_CONTAINER(window), button);
g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(on_button_clicked), window);
g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
这应该是这样的:
请注意,response_id为1以供日后参考。
让我们测试一个带有按钮的简单窗口,该按钮将通过其信号处理程序/回调触发对话:
gcc -o dialog main.c `pkg-config --cflags --libs gtk+-3.0`
使用标准响应ID以编程方式构造对话框时,可以使用预定义的枚举器来检查响应。使用glade,即使我们已经定义了Stock按钮,我们也必须定义(用户定义的)响应id,以便gtk_dialog_run函数能够理解用户已经选择了响应。
编译:
var children1 = document.getElementById('column1').getElementsByClassName('child');
应该会产生类似的结果:
答案 1 :(得分:2)
在与@Jose Fonte讨论后,我的问题的解决方案如下:
// Signal Handler for Clicking File -> Quit
void file_quit_clicked (__attribute__((unused)) GtkMenuItem *item, gpointer user_data)
{
GtkBuilder *builder = (GtkBuilder *) user_data;
GtkDialog *dialog = GTK_DIALOG(gtk_builder_get_object(builder, "quit_dialog"));
gtk_widget_show_all(GTK_WIDGET(dialog));
// This switch statement and gtk_widget_hide is needed so that the dialog window can be closed
// and reopened again without causing errors (i.e. if the user clicks "no" when asked "are you
// sure you want to quit." Without these, the window errors out the second time opened.
gint result = gtk_dialog_run (dialog);
switch (result)
{
case GTK_RESPONSE_ACCEPT:
// do_application_specific_something (); - Nothing Required
break;
default:
// do_nothing_since_dialog_was_cancelled (); - Nothing Required
break;
}
gtk_widget_hide(GTK_WIDGET(dialog));
}
将以下代码段插入主函数:
// So the "Are you sure you want to quit?" Dialog Box is hidden after clicking "x"
GtkDialog *quit_dialog = GTK_DIALOG(gtk_builder_get_object(builder, "quit_dialog"));
gtk_widget_hide_on_delete (GTK_WIDGET(quit_dialog));