2013-02-27 44 views
3

我決定製作一個玩具應用程序,通過向GtkGrid添加行/列元素來調整標籤表的大小。GtkGrid未顯示附加到它的新小部件

從按鈕回調函數中向GtkGrid添加新元素時,網格窗口小部件會更新其大小,但無法更新內容。

#include <gtk/gtk.h> 

static void kaboom(GtkWidget*, gpointer); 
static void minusVert(GtkWidget*, gpointer); 
static void plusVert(GtkWidget*, gpointer); 

static int x = 1; 
static int y = 1; 

int main(int argc, char** argv) { 
    GtkWidget* window, *subs, *buts, *grid, *button; 

    gtk_init(&argc, &argv); 

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_title(GTK_WINDOW(window), "Playground!!"); 
    gtk_widget_set_size_request(window, 200, 50); 

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(kaboom), NULL); 

    subs = gtk_paned_new(GTK_ORIENTATION_VERTICAL); 
    gtk_container_add(GTK_CONTAINER(window), subs); 

    grid = gtk_grid_new(); 
    gtk_paned_pack1(GTK_PANED(subs), grid, true, false); 

    gtk_grid_set_row_homogeneous(GTK_GRID(grid), true); 
    gtk_grid_set_column_homogeneous(GTK_GRID(grid), true); 

    gtk_grid_attach(GTK_GRID(grid), gtk_label_new("×"), 0, 0, 1, 1); 

    buts = gtk_grid_new(); 
    gtk_paned_pack2(GTK_PANED(subs), buts, false, false); 

    gtk_grid_set_row_homogeneous(GTK_GRID(grid), true); 
    gtk_grid_set_column_homogeneous(GTK_GRID(buts), true); 

    gtk_grid_attach(GTK_GRID(buts), gtk_label_new("Vertical"), 0, 0, 2, 1); 

    button = gtk_button_new_with_label("-"); 
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(minusVert), grid); 
    gtk_grid_attach(GTK_GRID(buts), button, 0, 1, 1, 1); 

    button = gtk_button_new_with_label("+"); 
    g_signal_connect(G_OBJECT(button), "clicked", G_CALLBACK(plusVert), grid); 
    gtk_grid_attach(GTK_GRID(buts), button, 1, 1, 1, 1); 

    gtk_grid_attach(GTK_GRID(buts), gtk_label_new("Horizontal"), 2, 0, 2, 1); 

    button = gtk_button_new_with_label("-"); 
    gtk_grid_attach(GTK_GRID(buts), button, 2, 1, 1, 1); 

    button = gtk_button_new_with_label("+"); 
    gtk_grid_attach(GTK_GRID(buts), button, 3, 1, 1, 1); 

    gtk_widget_show_all(window); 

    gtk_main(); 

    return 0; 
} 

static void kaboom(GtkWidget* window, gpointer data) { 
    gtk_main_quit(); 
} 

static void minusVert(GtkWidget *button, gpointer data) { 
    if (y > 1) { 
     // Remove all the elems in the last row 
     for (int i = 0; i < x; ++i) { 
      gtk_widget_destroy(gtk_grid_get_child_at(GTK_GRID(data), i, y - 1)); 
     } 
     --y; 
    } 
} 

static void plusVert(GtkWidget *button, gpointer data) { 
    for (int i = 0; i < x; ++i) { 
     // Add the new row 
     gtk_grid_attach(GTK_GRID(data), gtk_label_new("×"), i, y, 1, 1); 
    } 
    ++y; 
} 

截圖前後:

The screenshots before and after

我的問題是:我怎麼能做出連接到GtkGrid可見新標籤?

注意:我使用的是C++,但不是GTKmm,因爲我正在通過一本爲GTK +編寫的書。即使移植到純C,應用程序的行爲也是相同的。

+0

問題是什麼?到目前爲止,你嘗試了什麼,結果如何? – 2013-02-27 16:31:50

+0

@SimonMartin看來我原來的帖子真的很笨拙。抱歉。 – 2013-02-27 21:01:56

回答

3

當您創建小部件時,默認情況下不會顯示這些小部件。你必須告訴他們這樣做。您可以更改回調plusVert類似:

static void plusVert(GtkWidget *button, gpointer data) { 
    for (int i = 0; i < x; ++i) { 
    GtkWidget *label = gtk_label_new("×"); 
    gtk_grid_attach(GTK_GRID(data), label, i, y, 1, 1); 
    gtk_widget_show(label); 
    } 
    ++y; 
} 

gtk_widget_show()

+0

謝謝你回答我的問題,並使原來的提交更清晰。 – 2013-02-27 20:09:43