2011-04-03 128 views
1

如何從C++中的Gtkmm::Table中刪除一行?從Gtkmm ::表中刪除一行

文檔是:-(真的很差。我一直在使用resize()有沒有影響審判,也處於Gtk::Widget&我通過get_children()獲得調用remove,但我得到段錯誤...

+0

請顯示您的分段代碼。 Gtk ::容器刪除方法應該是這樣做的方式。 – Mark 2011-04-05 13:29:06

回答

1

如果Gtkmm::TableGtkTable然後gtk_container_remove應該,可能,你需要什麼,我做了一個小例子(見下文),它從表中刪除容器中的所有標籤控件。

#include <gtk/gtk.h> 

void remove_widgets(GtkWidget *widget, gpointer data) 
{ 
    if (GTK_IS_LABEL(widget)) 
    { 
     g_print("remove label\n"); 
     gtk_container_remove(GTK_CONTAINER(data), widget); 
    } 
} 

void remove_rows(GtkWidget *button, GtkWidget* data) 
{ 
    g_print("remove rows with labels\n"); 
    gtk_container_foreach(GTK_CONTAINER(data), remove_widgets, data); 
} 

int main(int argc, char *argv[]) 
{ 
    GtkWidget *window; 
    GtkWidget *table; 

    gtk_init(&argc, &argv); 

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); 
    gtk_widget_set_size_request (window, 300, 250); 
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 

    gtk_window_set_title(GTK_WINDOW(window), "Test Table Remove"); 

    gtk_container_set_border_width(GTK_CONTAINER(window), 15); 

    table = gtk_table_new(3, 2, FALSE); 
    gtk_table_set_col_spacings(GTK_TABLE(table), 3); 

    GtkWidget *label0 = gtk_label_new("Test Label 0"); 
    gtk_table_attach(GTK_TABLE(table), label0, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 0, 0); 
    GtkWidget *label1 = gtk_label_new("Test Label 1"); 
    gtk_table_attach(GTK_TABLE(table), label1, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 0, 0); 

    GtkWidget *label2 = gtk_label_new("Test Label 3"); 
    gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 0, 0); 
    GtkWidget *label3 = gtk_label_new("Test Label 4"); 
    gtk_table_attach(GTK_TABLE(table), label3, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 0, 0); 

    GtkWidget *button0 = gtk_button_new_with_label("Remove Labels"); 
    gtk_widget_set_size_request(button0, 70, 30); 
    gtk_table_attach(GTK_TABLE(table), button0, 0, 1, 2, 3, GTK_FILL, GTK_FILL, 0, 0); 

    g_signal_connect(G_OBJECT(button0), "clicked", G_CALLBACK(remove_rows), table); 

    gtk_container_add(GTK_CONTAINER(window), table); 

    g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), G_OBJECT(window)); 

    gtk_widget_show_all(window); 
    gtk_main(); 

    return 0; 
} 

希望這會有所幫助,至於