gtk3.0學習筆記4-構建使用者介面
構建使用者介面
當構建一個更復雜的使用者介面時,幾十個或幾百個小部件,在C程式碼中進行所有的安裝工作都是麻煩的,而且改變是不可能的。
值得慶幸的是,GTK +通過使用可由GtkBuilder類解析的XML格式的UI描述來支援使用者介面佈局與業務邏輯的分離。
建立一個test.c檔案,程式碼如下
#include <gtk/gtk.h>
static void
print_hello (GtkWidget *widget,
gpointer data)
{
g_print ("Hello World\n");
}
int
main (int argc,
char *argv[])
{
GtkBuilder *builder;
GObject *window;
GObject *button;
gtk_init (&argc, &argv);
/* 建立一個GtkBuilder instance載入UI檔案 */
builder = gtk_builder_new ();
gtk_builder_add_from_file (builder, "builder.ui", NULL);
/* Connect signal handlers to the constructed widgets. */
window = gtk_builder_get_object (builder, "window");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
// 獲取button1的物件 在UI檔案中為對應的object id
button = gtk_builder_get_object (builder, "button1");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
button = gtk_builder_get_object (builder, "button2");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
button = gtk_builder_get_object (builder, "quit" );
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_main ();
return 0;
}
void gtk_main()
一旦GTK+應用程式呼叫了gtk_main(),那麼GTK+就接管了控制權。gtk_main()函式執行主迴圈,直到呼叫gtk_main_quit()函式,gtk_main()才會退出。gtk_main()函式的所有例項功能都是一樣的,它們都監視同一個與X伺服器的連線,都對同樣的事件佇列起作用。gtk_main()例項用於阻塞、 遮斷一個函式的控制流直到滿足某些條件。所有的Gtk+程式都用這個技巧使應用程式正在執行時main()函式不能退出去。與while(1)類似
void gtk_main_quit()
gtk_main_quit()會使gtk_main()跳出迴圈並返回。
GTK+允許gtk_main()巢狀,每個gtk_main()呼叫必須對應於一個gtk_main_quit()呼叫。
guint gtk_main_level()
gtk_main_level()用來判斷gtk_main()當前級別。
如果返回0,說明沒有在gtk_main()環境中執行應用程式。如果返回1,說明有一個單獨的gtk_main()呼叫存在,等等。
gint gtk_main_iteration()
gtk_main_iteration()會使GTK+只在主迴圈中通過一次,處理下一個事件並允許構件呼叫訊號函式,但是不會呼叫init和quit函式
builder.ui檔案
類似XML的格式,至於其中的照著寫就行了,後面的我也還沒了解到。。。
<interface>
<object id="window" class="GtkWindow">
<property name="visible">True</property>
<property name="title">Grid</property>
<property name="border-width">10</property>
<child>
<object id="grid" class="GtkGrid">
<property name="visible">True</property>
<child>
<object id="button1" class="GtkButton">
<property name="visible">True</property>
<property name="label">First Button</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object id="button2" class="GtkButton">
<property name="visible">True</property>
<property name="label">Second Button</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object id="quit" class="GtkButton">
<property name="visible">True</property>
<property name="label">Quit</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
</object>
<packing>
</packing>
</child>
</object>
</interface>
請注意,GtkBuilder還可以用於構造不是小部件的物件,如樹模型,調整等。這就是我們在這裡使用的方法被呼叫 gtk_builder_get_object()並返回一個GObject 而不是GtkWidget *的原因。
通常,您將傳遞一個完整的路徑, gtk_builder_add_from_file()使程式的執行與當前目錄無關。安裝UI說明和類似資料的常見位置是 。 /usr/share/appname
也可以將原始碼中的UI描述作為字串嵌入並用於gtk_builder_add_from_string()載入它。但是將UI描述儲存在單獨的檔案中有幾個優點:然後可以對UI進行微調,而無需重新編譯程式,更重要的是,圖形UI編輯器(如glade) 可以載入檔案,並允許您建立和通過點選滑鼠來修改您的使用者介面。*