Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

c - How to replace the value of GtkEntry properly

Inside handlers clearHandle and replaceHandle i'm trying to replace the value of GtkEntry using gtk_entry_set_text function but it's not working for me.

Here is what i did

#include <gtk/gtk.h>

void clearHandle(GtkEntry *e)
{
    gtk_entry_set_text(e, "");
}

void replaceHandle(GtkEntry *e)
{
    gtk_entry_set_text(e, "Hello World");
}

static void activate(GtkApplication *app, gpointer user_data)
{
    GtkWidget *window;
    GtkWidget *button_box;
    GtkWidget *text_box;
    GtkWidget *clearButton;
    GtkWidget *replaceButton;
    GtkWidget *textInputBox;

    window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(window), "First GUI in c");
    gtk_window_set_default_size(GTK_WINDOW(window), 600, 600);

    // Containers and adding them to the window
    button_box = gtk_button_box_new(GTK_ORIENTATION_VERTICAL);
    text_box = gtk_fixed_new();

    // Textbox inside container
    textInputBox = gtk_entry_new();
    gtk_entry_set_text((GtkEntry *)textInputBox, "Hello boom World");

    clearButton = gtk_button_new_with_label("Clear");
    replaceButton = gtk_button_new_with_label("Replace with hello World");

    gtk_container_add(GTK_CONTAINER(button_box), textInputBox);
    gtk_container_add(GTK_CONTAINER(button_box), clearButton);
    gtk_container_add(GTK_CONTAINER(button_box), replaceButton);

    g_signal_connect(clearButton, "clicked", G_CALLBACK(clearHandle), (GtkEntry *)textInputBox);
    g_signal_connect(replaceButton, "clicked", G_CALLBACK(replaceHandle), (GtkEntry *)textInputBox);

    gtk_container_add(GTK_CONTAINER(window), button_box);

    gtk_widget_show_all(window);
}


int main(int argc, char **argv)
{
    GtkApplication *app;
    int status;

    app = gtk_application_new("org.yk.dev", G_APPLICATION_FLAGS_NONE);
    g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref(app);

    return status;
}


While Running this i'm getting following error in terminal

(main:14233): Gtk-CRITICAL **: 06:08:43.524: gtk_entry_set_text: assertion 'GTK_IS_ENTRY (entry)' failed

(main:14233): Gtk-CRITICAL **: 06:08:44.188: gtk_entry_set_text: assertion 'GTK_IS_ENTRY (entry)' failed


question from:https://stackoverflow.com/questions/65837798/how-to-replace-the-value-of-gtkentry-properly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The clicked signal gives two arguments: first the GtkButton * that was clicked, then the userdata pointer. Your callbacks need to take that GtkButton * as the first argument (even though you don't use it); currently it tries to use the button as the entry, which is why you get the "'GTK_IS_ENTRY (entry)' failed".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...