获取 gtk C 上表单输入的值

问题描述 投票:0回答:1

我有一个用C编写的gtk程序,我在模态中声明了一个表单,单击按钮后会显示该表单,我想从表单中获取输入的信息:

表格声明:

void on_button_add_clicked(GtkButton *button, gpointer user_data)
{
    GtkWindow *parent_window = GTK_WINDOW(user_data);

    // Create a new modal window
    GtkWidget *modal_window = gtk_window_new();
    gtk_window_set_title(GTK_WINDOW(modal_window), "Add Item");
    gtk_window_set_transient_for(GTK_WINDOW(modal_window), parent_window);
    gtk_window_set_modal(GTK_WINDOW(modal_window), TRUE);

    // Create a box to hold the labels and entries
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
    gtk_window_set_child(GTK_WINDOW(modal_window), box);

    // Set padding around the box container
    gtk_widget_set_margin_start(box, 40);
    gtk_widget_set_margin_end(box, 40);
    gtk_widget_set_margin_top(box, 40);
    gtk_widget_set_margin_bottom(box, 40);

    // Create and add the first name label and entry to the box
    GtkWidget *first_name_label = gtk_label_new("Enter first name:");
    GtkWidget *first_name_entry = gtk_entry_new();
    gtk_box_append(GTK_BOX(box), first_name_label);
    gtk_box_append(GTK_BOX(box), first_name_entry);

    // Create and add the second name label and entry to the box
    GtkWidget *second_name_label = gtk_label_new("Enter second name:");
    GtkWidget *second_name_entry = gtk_entry_new();
    gtk_box_append(GTK_BOX(box), second_name_label);
    gtk_box_append(GTK_BOX(box), second_name_entry);

    // Create and add the ID label and entry to the box
    GtkWidget *id_label = gtk_label_new("Enter ID (numbers only):");
    GtkWidget *id_entry = gtk_entry_new();
    gtk_entry_set_input_purpose(GTK_ENTRY(id_entry), GTK_INPUT_PURPOSE_DIGITS); // Sets input method for ID to accept only numbers
    gtk_box_append(GTK_BOX(box), id_label);
    gtk_box_append(GTK_BOX(box), id_entry);

    // Create and add "Confirm" button to the box
    GtkWidget *confirm_button = gtk_button_new_with_label("Confirm");
    gtk_box_append(GTK_BOX(box), confirm_button);

    // Present the modal window
    gtk_window_present(GTK_WINDOW(modal_window));
}

我添加了确认信号并尝试使用 gtk_entery_get_text 从中获取信息:

void on_confirm_button_clicked(GtkButton *button, gpointer user_data)
{
    // Retrieve the modal window and input fields from user_data
    GtkWidget *modal_window = gtk_widget_get_ancestor(GTK_WIDGET(button), GTK_TYPE_WINDOW);
    GtkWidget *first_name_entry = g_object_get_data(G_OBJECT(modal_window), "first_name_entry");
    GtkWidget *second_name_entry = g_object_get_data(G_OBJECT(modal_window), "second_name_entry");
    GtkWidget *id_entry = g_object_get_data(G_OBJECT(modal_window), "id_entry");

    // Get text from the input fields
    const char *first_name = gtk_entry_get_text(GTK_ENTRY(first_name_entry));
    const char *second_name = gtk_entry_get_text(GTK_ENTRY(second_name_entry));
    const char *id = gtk_entry_get_text(GTK_ENTRY(id_entry));

    // // Save data to the array
    if (people_count < 100) {  // Ensure we don't exceed the array size
        strncpy(people[people_count].first_name, first_name, sizeof(people[people_count].first_name) - 1);
        strncpy(people[people_count].second_name, second_name, sizeof(people[people_count].second_name) - 1);
        strncpy(people[people_count].id, id, sizeof(people[people_count].id) - 1);
        people_count++;
    }

    // // Close the modal window
    gtk_window_close(GTK_WINDOW(modal_window));

    // // Print the contents of the array to the terminal
    for (int i = 0; i < people_count; i++) {
        printf("Person %d: %s %s, ID: %s\n", i + 1, people[i].first_name, people[i].second_name, people[i].id);
    }
}

它不起作用,我遇到了很多编译错误: compilation errors

我使用的编译命令:gcc $( pkg-config --cflags gtk4 ) -o app tov.c $( pkg-config --libs gtk4 )

如何解决这个问题

c user-interface gtk gtk3 gtk4
1个回答
0
投票

不幸的是,您没有提供完整的清单。否则,我可以返回一个工作版本。 所以只有一些提示: GTK4 中不再存在 gtk_entry_get_text 函数。相反,您应该像我在回答问题https://stackoverflow.com/a/77697643/22768315我所描述的那样进行操作。 (使用缓冲液) 据推测,您所显示的脚本中不同函数内的变量的有效性也存在问题。同样,以下答案可以提供帮助:

https://stackoverflow.com/a/77634992/22768315

问候。

© www.soinside.com 2019 - 2024. All rights reserved.