无法通过单击C中的按钮gtk4来向框添加标签

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

我有以下代码,其中有一个包含标签、条目和按钮的框。 我为添加包含 EntryBuffer 中文本的标签的按钮设置了一个回调函数。 当我运行它时,在条目中输入一些内容,然后单击按钮没有任何反应。

#include <gtk/gtk.h>
#undef main

struct MyWidgets{
  GtkWidget* prompt;
  GtkWidget* box;
};

void on_button_clicked(GtkButton *button, gpointer data) {
    struct MyWidgets *widgets = (struct MyWidgets*) data;

    GtkEntryBuffer *buffer = gtk_entry_get_buffer((GtkEntry*)widgets->prompt);

    const char *text = gtk_entry_buffer_get_text(buffer);
  
    GtkWidget *output = gtk_label_new(text);
    gtk_widget_set_margin_start(output, 30);

    gtk_box_append((GtkBox*)widgets->box, output);

    gtk_widget_set_visible(output, TRUE);
}

static void on_activate (GtkApplication *app) {
    // Create a new window
    GtkWidget *window = gtk_application_window_new (app);

    // Set the default size of the window to 500x300
    gtk_window_set_resizable((GtkWindow*)window, FALSE);
    gtk_window_set_default_size((GtkWindow*)(window), 800, 900);

    // Create a vertical box to organize elements
    GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10);
    gtk_box_set_homogeneous((GtkBox*)box, FALSE);

    //create label
    GtkWidget *label = gtk_label_new("Enter ebnf expression :");
    gtk_label_set_xalign((GtkLabel*)label, 0);
    gtk_widget_set_margin_start(label, 30);

    // create input box 
    GtkWidget *prompt = gtk_entry_new();
    
    gtk_widget_set_size_request(prompt, 300, 40);
    gtk_widget_set_margin_start(prompt, 30);
    gtk_widget_set_margin_end(prompt, 30);

    // Create a new button
    GtkWidget *button = gtk_button_new_with_label ("Submit");
    gtk_button_set_has_frame((GtkButton*)button, TRUE);
    gtk_widget_set_margin_start(button, 300);
    gtk_widget_set_margin_end(button, 300);

    // Create a data structure to hold multiple pieces of data
    
    struct MyWidgets widgets;
    widgets.box = box;
    widgets.prompt = prompt;

    // Pack the text view and button into the vertical box
    gtk_box_append((GtkBox*)box, label);
    gtk_box_append((GtkBox*)box, prompt);
    gtk_box_append((GtkBox*)box, button);

    // When the button is clicked, close the window passed as an argument
    g_signal_connect((GtkButton*)button, "clicked", G_CALLBACK(on_button_clicked),&widgets);
    


    // Set the box as the child of the window
    gtk_window_set_child(GTK_WINDOW(window), box);

    gtk_window_present (GTK_WINDOW (window));
}

// main ....

我是gtk4的新手,我想知道问题是否在于框中的元素未在激活功能中更新。旧版本中我猜有一个 show.all 方法,但是在 gtk4 中有 set_visible,我已经在回调函数中为 label 设置了它,但仍然没有解决问题

c callback gtk gnu gtk4
1个回答
0
投票

这里已经有一个类似的问题: Passingcallbackarguments through GTK's g_signal_connect()会导致C中出现意外的值, Ted Lyngmo 在这里回答了这个问题。 https://stackoverflow.com/a/77149801 对于您的问题,我建议进行以下更改:

// Create a data structure to hold multiple pieces of data
    static MyWidgets widgets;
    MyWidgets *widgets_ptr =&widgets ;
    widgets_ptr->box = box;
    widgets_ptr->prompt = prompt;

// When the button is clicked, close the window passed as an argument
g_signal_connect((GtkButton*)button, "clicked", G_CALLBACK(on_button_clicked),widgets_ptr);
© www.soinside.com 2019 - 2024. All rights reserved.