GTK+中文社区(gtk.awaysoft.com)

 找回密码
 马上加入

QQ登录

只需一步,快速开始

查看: 4105|回复: 2

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

[复制链接]
  • TA的每日心情
    奋斗
    2015-10-16 12:17
  • 签到天数: 401 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2013-8-26 00:49:50 | 显示全部楼层 |阅读模式

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



    然后打开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;
    }


    编译运行  点击不同按钮   会输出不同的文字


    如图:




    以上对于按钮的回调仅用来演示   实际运用当中  应如用以下代码判断

    if (GTK_TOGGLE_BUTTON (widget)->active)
    {
    /* 如果执行这段程序,表明按钮是选中或者按下的* /
    } else {
    /* 如果执行这段程序,按钮是没有选中或者弹起的* /
    }


    本文出自 bailangcn's,转载时请注明出处及相应链接



    *滑块验证:
    您需要登录后才可以回帖 登录 | 马上加入

    本版积分规则

    申请友链|Archiver|小黑屋|手机版|GTK+中文社区 ( 粤ICP备13080851号 )

    我要啦免费统计

    GMT+8, 2024-12-22 01:22 , Processed in 0.045451 second(s), 9 queries , Redis On.

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表