Linux 下從頭再走 GTK+-3.0 (五),gtk-3.0
實踐中表明,純粹利用 gtk 函數來建立 UI 是很繁瑣的事,需要編寫很多代碼。怎樣才能快速統一的建立 UI 布局呢?
可喜的是 GTK 提供了一個 GtkBuilder 用於快速建立介面。它讀取一個 xml 檔案。按照 xml 檔案中所描述的來建立介面。因此我們需要編寫 xml 檔案。
建立example.ui ,內容如下:
<?xml version="1.0" encoding="UTF-8"?><interface> <object id="window" class="GtkWindow"> <property name="visible">True</property> <property name="title">GtkBuilder</property> <property name="border-width">30</property> <child> <object id="grid" class="GtkGrid"> <property name="visible">True</property> <child> <object id="print_hello" class="GtkButton"> <property name="visible">True</property> <property name="label">Button 1</property> </object> <packing> <property name="left-attach">0</property> <property name="top-attach">0</property> </packing> </child> <child> <object id="print_entry" class="GtkButton"> <property name="visible">True</property> <property name="label">Button 2</property> </object> <packing> <property name="left-attach">1</property> <property name="top-attach">0</property> </packing> </child> <child> <object id="entry" class="GtkEntry"> <property name="visible">True</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>
再建立源檔案 example.c ,內容如下:
#include <gtk/gtk.h>static void print_hello(GtkWidget *button , gpointer data);static void activate(GtkApplication *app , gpointer data);static void print_entry(GtkWidget * button , gpointer entry);int main(int argc , char **argv){ GtkApplication *app; int app_status; app = gtk_application_new("org.rain.example" , G_APPLICATION_FLAGS_NONE); g_signal_connect(app , "activate" , G_CALLBACK(activate) , NULL); app_status = g_application_run(G_APPLICATION(app) , argc , argv); g_object_unref(app); return app_status;}static void print_hello(GtkWidget *button , gpointer data){ printf("Hello,world!\n");}static void print_entry(GtkWidget * button , gpointer entry){ const gchar *entry_buf; //擷取輸入框的內容,返回一個 const gchar 指標 , gchar 即是 char entry_buf = gtk_entry_get_text( GTK_ENTRY(entry) ); if('\0' != entry_buf[0]) printf("%s\n" , entry_buf);}static void activate(GtkApplication *app , gpointer data){ GtkBuilder *builder; GObject *window; GObject *button; GObject *entry; //建立一個 GtkBuilder 。 builder = gtk_builder_new(); //將描述 UI 的檔案添加到 Builder 中。 gtk_builder_add_from_file(builder , "example.ui" , NULL); //可以用該函數擷取 Builder 建立的對象,根據 example.ui 檔案中設定的 object 的 id 來擷取。 window = gtk_builder_get_object(builder , "window"); gtk_application_add_window(app , GTK_WINDOW(window)); //注意該函數返回的是一個指向 GObject 對象的指標。 button = gtk_builder_get_object(builder , "print_hello"); //設定按鈕標籤 gtk_button_set_label ( GTK_BUTTON (button) , "PrintHello"); g_signal_connect(button , "clicked" , G_CALLBACK(print_hello) , NULL); //擷取輸入框對象。 entry = gtk_builder_get_object(builder , "entry"); button = gtk_builder_get_object(builder , "print_entry"); gtk_button_set_label ( GTK_BUTTON (button) , "PrintEntry"); //調用 print_entry 時傳遞使用者參數 entry 。 g_signal_connect(button , "clicked" , G_CALLBACK(print_entry) , entry); }
編譯,運行:
gcc example.c `pkg-config --cflags --libs gtk+-3.0`./a.out
運行結果如下: