GtkRadioButton示例
#include<gtk/gtk.h>
GtkWidget *window,*hbox,*button1,*button2,*button3;
void toggled(GtkWidget *widget, gpointer data)
{
if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget)))
{
g_print ("%s\n", data);
}
}
void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc,char** argv)
{
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window),"Radio button");
gtk_container_set_border_width(GTK_CONTAINER(window),3);
g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),0);
hbox=gtk_hbox_new(TRUE,3);
/* 单选按钮的创建 */
button1=gtk_radio_button_new_with_label(NULL,"orange");
g_signal_connect(G_OBJECT(button1),"toggled",G_CALLBACK(toggled),"orange");
/* 每创建一个单选按钮时,都必须调用一次gtk_radio_button_get_group,否则出错*/
button2=gtk_radio_button_new_with_label(gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)),"apple");
g_signal_connect(G_OBJECT(button2),"toggled",G_CALLBACK(toggled),"apple");
button3=gtk_radio_button_new_with_label(gtk_radio_button_get_group(GTK_RADIO_BUTTON(button1)),"banana");
g_signal_connect(G_OBJECT(button3),"toggled",G_CALLBACK(toggled),"banana");
/* 设置第三个按钮为激活状态 */
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button3),TRUE);
/* 将单选按钮添加到hbox内 */
gtk_box_pack_start(GTK_BOX(hbox),button1,TRUE,TRUE,3);
gtk_box_pack_start(GTK_BOX(hbox),button2,TRUE,TRUE,3);
gtk_box_pack_start(GTK_BOX(hbox),button3,TRUE,TRUE,3);
gtk_container_add(GTK_CONTAINER(window),hbox);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
GTK+学习笔记-树型视图与模型(GtkTreeView)
GtkTreeStore *lefttree = NULL;
树型模型的建立:
{
lefttree = gtk_tree_store_new(1,G_TYPE_STRING);
GtkTreeIter iter ;
GtkTreeIter child;
gtk_tree_store_append (lefttree, &iter, NULL);//建立一个空行
gtk_tree_store_set (lefttree, &iter ,0,"root1",-1);//为此行设置值
gtk_tree_store_append (lefttree, &iter, NULL);//建立另一个空行
gtk_tree_store_set (lefttree, &iter ,0,"root2",-1);//为此行设置值
gtk_tree_store_append (lefttree, &child, &iter);//为上一行建立一个子行
gtk_tree_store_set (lefttree, &child ,0,"child1",-1);//为此子行设置值
//更多行依此类推..........
}
树型视图的建立以及与数据模型的关联:
GtkCellRenderer *renderer;
GtkTreeViewColumn *column;
//关联视图与模型
gtk_tree_view_set_model(GTK_TREE_VIEW(mytree),GTK_TREE_MODEL(lefttree));
//将数据模型交给视图管理,视图销毁时数据会被一同销毁
g_object_unref (G_OBJECT (lefttree));
//建立一个GtkCellRenderer
renderer = gtk_cell_renderer_text_new ();
//设置GtkCellRenderer的渲染方式
g_object_set (G_OBJECT (renderer),
"foreground", "red",
NULL);
//建立一个带标题的列 并且将renderer放入其中使其能显示内容
column = gtk_tree_view_column_new_with_attributes ("Family", renderer,
"text", 0,
NULL);
//将列设置为可以手动改变大小
gtk_tree_view_column_set_resizable(column,TRUE);
//将列加入gtk_tree_view
gtk_tree_view_append_column (GTK_TREE_VIEW (mytree), column);
gtk_widget_show_all(mytree);
下面为建立好的树型视图实现一个功能:
当用户选中一个有子行的行时按Enter或Space键展开或收起子行,当选中一个没有子行的行时按Enter或Space键后台打印一些信息。
TreeView的"row-activated"信号产生条件:
--在gtk_tree_view_row_activated()被调用时
-- 一个行被双击时
-- 选中一个不可编辑的行,按下Space, Shift+Space, Return,Enter键时
*/
//此回调函数与 TreeView的"row-activated"信号相连接
void on_lefttree_row_activated (GtkTreeView *tree_view,
GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer user_data)
...{
GtkTreeIter iter ;
//根据path获得iter
if (gtk_tree_model_get_iter(GTK_TREE_MODEL(lefttree), &iter, path))
...{
//得到iter,判断iter指向的行是否有子行
if ( gtk_tree_model_iter_has_child (GTK_TREE_MODEL(lefttree), &iter) )
...{
//有子行,展开本行
if ( gtk_tree_view_expand_row(tree_view,path,FALSE) )
...{
//展开本行成功
g_print("expand child ");
}
else //本行已经被展开
...{
//收起本行
gtk_tree_view_collapse_row(tree_view,path);
g_print("collapse child ");
}
}
else
...{
//没有子行,直接执行规定的功能
g_print("do some ");
}
}
}
获得treeview中选中的一行的GtkTreeIter或path:
gtk_tree_selection_select_path( select, path );
gtk_tree_selection_select_iter( select, &iter );
要想更改GtkTreeIter指向的数据的值,使用:
GtkTreeView本身是不带横纵滚动条的,把他放入GtkScrolledWindow中就可以了,滚动条会随内容自动调整。