从 Gtkmm::Table 中删除一行

发布于 2024-10-29 04:37:54 字数 204 浏览 1 评论 0原文

如何在 C++ 中从 Gtkmm::Table 中删除一行?

文档真的很差:-(。我尝试使用 resize() 但没有任何效果,并且还在 Gtk::Widget& 处调用 remove > 我通过 get_children() 获得的,但是我得到了段错误...

How do I erase a row from a Gtkmm::Table in C++?

Documentation is really poor :-(. I have tried using resize() with no effect, and also calling remove at a Gtk::Widget& that I obtained through get_children(), but the I get segfault...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梨涡 2024-11-05 04:37:54

如果 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;
}

希望这有帮助,问候

If Gtkmm::Table is GtkTable then gtk_container_remove should, probably, do what you need. I've made a small example (see below) it removes all label widget from the table container.

#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;
}

hope this helps, regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文