admin 管理员组文章数量: 887021
实践中表明,纯粹利用 gtk 函数来创建 UI 是很繁琐的事,需要编写很多代码。怎样才能快速统一的建立 UI 布局呢?
可喜的是 GTK 提供了一个 GtkBuilder 用于快速创建界面。它读取一个 xml 文件。按照 xml 文件中所描述的来创建界面。因此我们需要编写 xml 文件。
创建example.ui ,内容如下:
True
GtkBuilder
30
True
True
Button 1
0
0
True
Button 2
1
0
True
0
1
2
再创建源文件 example.c ,内容如下:
#include
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[])
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
运行结果如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAApQAAAGlCAYAAABEAWapAAAABHNCSVQICAgIfAhkiAAAIABJREFUeF7snQd8VNeV/49QL6hLSAIJEAgEopgmOjY2xiW4xCVxiWuyabvZJJtkN/lnEyfrZHezLc2OW2IblyQuca8YV8BguuhdINRR713/c670hjdv3pt5I82IkfQ7+Uysee++W753Rvx0zj33Bi1fc2VvW0s79fb0EpG8YCAAAiAAAiAAAiAAAiBgn8CYkDEhLCZ7+AmISfvYUBIEQAAEQAAEQAAEQEAjMKaxoQk0QAAEQAAEQAAEQAAEQGDABMbAMzlgdngQBEAABEAABEAABECACbCghIEACIAACIAACIAACIDAwAlAUA6cHZ4EARAAARAAARAAARBgAhCU+BiAAAiAAAiAAAiAAAgMigAE5aDw4WEQAAEQAAEQAAEQAAEISnwGQAAEQAAEQAAEQAAEBkUAgnJQ+PAwCIAACIAACIAACIAABCU+AyAAAiBwgQlMzp5C11x/I4WHh1/gnqB5EAABEBgYAQjKgXHDUyAAAiDgMwLp48dTddU5am9v91mddiqaPmMmLcxfbKcoyoAACICAWwIQlG7x4CYIgAAI+JeAeCWTklKotLTEqaHQsDAaM8a/v6JjYsbS2LGx/h0gagcBEBgVBEJGxSgxSBAAARAIUAJp6RkUFERU3i8o85cso4TERBX+7unppdOFJ+nAvgLT3oeGhtKMvFk0blw6hYSEUG1tDR08sI8aGxooMjKS1lxxFe3dvYvOFp1Rz0tofdacufTOm6/TyksupZiYGHX92s/fqP77/oZ3qbkZx/EqGDAQAAGvCEBQeoULhUEABEDAtwQyxk+g6uoqamtrUxXX1dZS8dkz1NLSSuPS0mh67gwVDi8rLXVqOIhV6OKlyymCheOh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hfaxnHb52TdtzheFiI22HXMxr7hPQiAAAiAgO8J+FRQytpFCWtLiEq8KGWlxVRXV0s33XIbbXz3beUpjE9IUKM4eeK4z0Yj29iI909vxWfPUmHhSdX+lZ+7ht596w0V0pZQnJXJWrbcmXk0d9589h72qBCjr7KHTxw/Sq0tLSrUKx6eUt0WQvr+nOFwoidmZRwKrKqqVPv+nTp5XCXNSPKMiGapX0KrsrbQFyah9Hu+8nUVqpcQu3gqJzHDjIwJ9Eb/WlNPfRYBLSZ9ioqKVkseJHvbyk5zOFsYrLv287SBvbDnKit5DWWKI8Rt9dxQXPc01qHog74Nd2yN38eiM4VDthbREydj3+S7JyZZ+xGRkSqz/ypeFytLGE4Y9rwcasZoDwRAAARAwDMBHwvKdrX+STJwZZ/Chx/4Lf2FM1clW/e2O+/hCF+QElXb2UvlS0Ep27vIS2/aZs7S/nUccr/j7q+o25Jdq7KjTUyyorNYLF297jqVSCRr9HwlKEWIiddw0eKlfckyFn0QIe6RmexvyR5OSS7SPJEyJtnyZilvPL2vwHfZzpG8plQE8AwW2pKZLu3I+xf++qxayyfmqc8nWfRKQo6svRSTdZG11dbZ87IDgCTjXHv9jXT7nfeqZ2o5y1uu2fI0qyf8Y57
版权声明:本文标题:linux ctg重装,Linux 下从头再走 GTK+-3.0 (五) 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/jishu/1724521848h755466.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论