白浪 发表于 2013-8-26 00:49:50

glade3教程(四)--搞定按钮

打开glade3 新建一个窗口,在窗口中放置一个固定fixed然后再放置三个检查按钮 三个单选按钮放置一个双态按钮   然后分别在检查按钮单选按钮双态按钮下面分别放置一个标签并且将单选按钮2 单选按钮3 加入单选按钮1的组别中 保存为button.glade如图所示:http://www.bailangcn.tk/content/uploadfile/201203/thum-bdd531b68cda2a2ddbe47f8469c42e3820120319132210.png

然后打开code::blocks 输入以下代码    代码比较简单就不加注释了/*
作者:bailangcn时间:2012/3/19作者主页:bailangcn.tk*/
#include <gtk/gtk.h>GtkWidget *label1;
GtkWidget *label2;
GtkWidget *label3;
void set_text_to_label1(GtkWidget *Widget,gpointer *data)
{    gtk_label_set_text(GTK_LABEL(label1),(char *)data);
}
void set_text_to_label2(GtkWidget *Widget,gpointer *data)
{    gtk_label_set_text(GTK_LABEL(label2),(char *)data);
}
void set_text_to_label3(GtkWidget *Widget,gpointer *data)
{    gtk_label_set_text(GTK_LABEL(label3),(char *)data);
}
int main(int argc, char *argv[])
{
    GtkBuilder *builder;    gtk_init(&argc, &argv);
    GtkWidget *window;    GtkWidget *checkbutton1;
    GtkWidget *checkbutton2;
    GtkWidget *checkbutton3;
    GtkWidget *radiobutton1;
    GtkWidget *radiobutton2;
    GtkWidget *radiobutton3;
    GtkWidget *togglebutton;    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "button.glade", NULL);    window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
    checkbutton1 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton1"));
    checkbutton2 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton2"));
    checkbutton3 = GTK_WIDGET(gtk_builder_get_object(builder,"checkbutton3"));
    radiobutton1 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton1"));
    radiobutton2 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton2"));
    radiobutton3 = GTK_WIDGET(gtk_builder_get_object(builder,"radiobutton3"));
    togglebutton = GTK_WIDGET(gtk_builder_get_object(builder,"togglebutton"));
    label1 = GTK_WIDGET(gtk_builder_get_object(builder,"label1"));
    label2 = GTK_WIDGET(gtk_builder_get_object(builder,"label2"));
    label3 = GTK_WIDGET(gtk_builder_get_object(builder,"label3"));
    g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
    g_signal_connect(G_OBJECT(checkbutton1),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton1");
    g_signal_connect(G_OBJECT(checkbutton2),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton2");
    g_signal_connect(G_OBJECT(checkbutton3),"pressed",G_CALLBACK(set_text_to_label1),"you check checkbutton3");
    g_signal_connect(G_OBJECT(radiobutton1),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton1");
    g_signal_connect(G_OBJECT(radiobutton2),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton2");
    g_signal_connect(G_OBJECT(radiobutton3),"pressed",G_CALLBACK(set_text_to_label2),"you check radiobutton3");
    g_signal_connect(G_OBJECT(togglebutton),"toggled",G_CALLBACK(set_text_to_label3),"toggle");   // gtk_builder_connect_signals(builder, NULL);//自动关联已在 UI 文件配置好的信号如果你不在galde文件里面配置信号这行代码可以不用
    g_object_unref(G_OBJECT(builder));    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}
编译运行点击不同按钮   会输出不同的文字
如图:
http://www.bailangcn.tk/content/uploadfile/201203/thum-e8da1c4f26588d00af2fe9c4973afd0920120319132658.png

以上对于按钮的回调仅用来演示   实际运用当中应如用以下代码判断if (GTK_TOGGLE_BUTTON (widget)->active)
{
/* 如果执行这段程序,表明按钮是选中或者按下的* /
} else {
/* 如果执行这段程序,按钮是没有选中或者弹起的* /
}
本文出自 bailangcn's,转载时请注明出处及相应链接


白浪 发表于 2013-8-26 00:50:05

国际惯例。。。

colben 发表于 2014-5-1 22:13:16

页: [1]
查看完整版本: glade3教程(四)--搞定按钮