1 回答
TA贡献1895条经验 获得超3个赞
你要的API是gtk_container_set_chain
void gtk_container_set_focus_chain(GtkContainer *container,
GList *focusable_widgets);
focusable_widgets就是tab顺序的控件列表,必须是container的子控件,GList应该会用吧。
如果要设置不同容器中控件的顺序,嵌套就行;只是不能有跨控件的顺序。我实现了一个简单的程序来演示TAB顺序的设置。如下:
#include <gtk/gtk.h>
int main(int argc,char *argv[])
{
gtk_init(&argc,&argv);
GtkWidget *window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window),480,360);
g_signal_connect(G_OBJECT(window),"destroy",
G_CALLBACK(gtk_main_quit),NULL);
GtkWidget *box=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
GtkWidget *box1=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
GtkWidget *box2=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);
gtk_container_add(GTK_CONTAINER(window),box);
gtk_box_pack_start(GTK_BOX(box),box1,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box),box2,TRUE,TRUE,0);
GtkWidget *entry1,*entry2,*entry3,*entry4,*entry5,*entry6;
GList *chain;
entry1=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry1),"entry1");
entry2=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry2),"entry2");
entry3=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry3),"entry3");
entry4=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry4),"entry4");
entry5=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry5),"entry5");
entry6=gtk_entry_new();
gtk_entry_set_text(GTK_ENTRY(entry6),"entry6");
gtk_box_pack_start(GTK_BOX(box1),entry1,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box1),entry2,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box1),entry3,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box2),entry4,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box2),entry5,TRUE,TRUE,0);
gtk_box_pack_start(GTK_BOX(box2),entry6,TRUE,TRUE,0);
/*
* 下面设置TAB顺序,不能设置跨容器的顺序
* 比如524136,634251都不行
* 但是可以132465,或者645312
* 下面设置TAB顺序为564213
*/
/* 先设置两个GtkBox的顺序 */
chain=NULL;
chain=g_list_append(chain,box2);
chain=g_list_append(chain,box1);
gtk_container_set_focus_chain(GTK_CONTAINER(box),chain);
g_list_free(chain);
/* 设置box2中的子控件顺序564 */
chain=NULL;
chain=g_list_append(chain,entry5);
chain=g_list_append(chain,entry6);
chain=g_list_append(chain,entry4);
gtk_container_set_focus_chain(GTK_CONTAINER(box2),chain);
g_list_free(chain);
/* 设置box1中的子控件顺序213 */
chain=NULL;
chain=g_list_append(chain,entry2);
chain=g_list_append(chain,entry1);
chain=g_list_append(chain,entry3);
gtk_container_set_focus_chain(GTK_CONTAINER(box1),chain);
g_list_free(chain);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
- 1 回答
- 0 关注
- 835 浏览
添加回答
举报