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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…