GTK大杂烩

安装GTK

  • brew install gtk3

测试环境: msys32

前提:gtk3已装好

注意:CMakeLists写好并reload成功后,IDE才能根据include_directories指令找着GTK3的头文件,进行代码提示

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
cmake_minimum_required(VERSION 3.8)

project(untitled)

set(CMAKE_C_STANDARD 99)

find_package(PkgConfig REQUIRED)
pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
include_directories(${GTK3_INCLUDE_DIRS})
link_directories(${GTK3_LIBRARY_DIRS})
add_definitions(${GTK3_CFLAGS_OTHER})

set(SOURCE_FILES main.c)

add_executable(untitled ${SOURCE_FILES})

target_link_libraries(untitled ${GTK3_LIBRARIES})

main.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <gtk/gtk.h>

static void activate(GtkApplication *app,
gpointer user_data) {
GtkWidget *window;
window = gtk_application_window_new(app);
gtk_window_set_title(GTK_WINDOW(window), "Hello world");
gtk_widget_show_all(window);
}

int main(int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new("hello.world", G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return (status);
}

信号与槽

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <gtk/gtk.h>

static void say_hello() {
g_print("Hello World\n");
}

int main(int argc, char *argv[]) {
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Hello World");
gtk_window_set_default_size(GTK_WINDOW(window), 600, 400);

GtkWidget *button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
gtk_container_add(GTK_CONTAINER(window), button_box);

GtkWidget *button = gtk_button_new_with_label("Click Me");
gtk_container_add(GTK_CONTAINER(button_box), button);

g_signal_connect(button, "clicked", G_CALLBACK(say_hello), NULL);

gtk_widget_show_all(window);
gtk_main();
return 0;
}

GTK布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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_title(GTK_WINDOW(window), "Hello World");
gtk_window_set_default_size(GTK_WINDOW(window), 900, 400);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER_ALWAYS);

GtkWidget *box1 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *button1 = gtk_button_new_with_label("Button One");
GtkWidget *button2 = gtk_button_new_with_label("Button Two");
GtkWidget *button3 = gtk_button_new_with_label("Button Three");
gtk_widget_set_hexpand(button1, TRUE);
gtk_widget_set_hexpand(button2, TRUE);
gtk_widget_set_hexpand(button3, TRUE);
gtk_container_add(GTK_CONTAINER(box1), button1);
gtk_container_add(GTK_CONTAINER(box1), button2);
gtk_container_add(GTK_CONTAINER(box1), button3);

GtkWidget *box2 = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
GtkWidget *button4 = gtk_button_new_with_label("Button Four");
GtkWidget *button5 = gtk_button_new_with_label("Button Five");
gtk_widget_set_hexpand(button4, TRUE);
gtk_widget_set_hexpand(button5, TRUE);
gtk_container_add(GTK_CONTAINER(box2), button4);
gtk_container_add(GTK_CONTAINER(box2), button5);

GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
g_signal_connect(window, "destroy", gtk_main_quit, NULL);

gtk_widget_set_vexpand(box1, TRUE);
gtk_widget_set_vexpand(box2, TRUE);
gtk_container_add(GTK_CONTAINER(box), box1);
gtk_container_add(GTK_CONTAINER(box), box2);
gtk_container_add(GTK_CONTAINER(window), box);

gtk_widget_show_all(window);
gtk_main();
return 0;
}

TreeView示例

GTK的ListBox是布局容器,不是用来组件复用的。得用TreeView

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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_title(GTK_WINDOW(window), "Hello World");
gtk_window_set_default_size(GTK_WINDOW(window), 900, 400);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

GtkWidget *treeView = gtk_tree_view_new();
GtkCellRenderer *renderer = gtk_cell_renderer_text_new();
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeView), -1, "ID", renderer, "text", 0, NULL);
gtk_tree_view_insert_column_with_attributes(GTK_TREE_VIEW(treeView), -1, "Name", renderer, "text", 1, NULL);
GtkListStore *listStore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_STRING);
gtk_tree_view_set_model(GTK_TREE_VIEW(treeView), GTK_TREE_MODEL(listStore));
gtk_container_add(GTK_CONTAINER(window), treeView);

GtkTreeIter iter;
gtk_list_store_append(listStore, &iter);
gtk_list_store_set(listStore, &iter, 0, "one", 1, "Hello", -1);
gtk_list_store_append(listStore, &iter);
gtk_list_store_set(listStore, &iter, 0, "two", 1, "World", -1);
gtk_widget_show_all(window);

g_signal_connect(window, "destroy", gtk_main_quit, NULL);
gtk_main();
return 0;
}