Assertion 'GTK_IS_ENTRY (entry)' failed

Hello everyone! I write a translator for morse. I faced a problem moving from gtk3 in gtk4 with GtkEntry. As I understood there is no GTK_ENTRY macro anymore and I also should use GtkEntryBuffer to get text from entry.
Everything is fine 'til I work with one entry. But if I use two entry and try to redefine it from gpointer type it crashes: gtk_entry_get_buffer: assertion ‘GTK_IS_ENTRY (entry)’ failed

Now all I want is program gets two string from entryIn and entryOut and write it into prompt.

Thank you for your help and attention,
Johann

P.S. This code includes GTK_ENTRY macro. I checked it up one more time with (GtkEntry *) instead of macro and it still doesn’t work

void print_text(GtkWidget *Widget, gpointer data) {
    GtkEntry **Entries = (GtkEntry **) data;
    GtkEntry *EntryIn = Entries[0];
    //GtkEntry *EntryOut = Entries[1];
    GtkEntryBuffer *BufferIn = gtk_entry_get_buffer(EntryIn);
    //GtkEntryBuffer *BufferOut = gtk_entry_get_buffer(EntryOut);
    const char *textIn = gtk_entry_buffer_get_text(BufferIn);
    const char *textOut = gtk_entry_buffer_get_text(BufferOut);
    printf("%s\n", textIn);
    printf("%s\n", textOut);
}

static void activate(GtkApplication* app, gpointer user_data) {
    GtkWidget *Window;
    GtkWidget *Button;
    GtkWidget *Box;
    GtkWidget *EntryIn, *EntryOut;
    GtkEntryBuffer *EntryBufferIn, *EntryBufferOut;
    GtkEntry *Entries[2];

    Window = gtk_application_window_new(app);
    gtk_window_set_title(GTK_WINDOW(Window), "test");
    gtk_window_set_default_size(GTK_WINDOW(Window), 300, 400);
    gtk_window_present(GTK_WINDOW(Window));

    EntryBufferIn = gtk_entry_buffer_new(NULL, -1);
    EntryIn = gtk_entry_new_with_buffer(EntryBufferIn);
    
    EntryBufferOut = gtk_entry_buffer_new(NULL, -1);
    EntryOut = gtk_entry_new_with_buffer(EntryBufferOut);

    Entries[0] = GTK_ENTRY(EntryIn);
    Entries[1] = GTK_ENTRY(EntryOut);

    Button = gtk_button_new_with_label("Read");
    g_signal_connect(Button, "clicked", G_CALLBACK(print_text), Entries);

The Entries array is local to your activate() function; you cannot use it inside the print_text() callback.

If you want to set up an array of entries and use it inside a callback, you’ll have to allocate it, like:

GtkEntry **entries = g_new (GtkEntry*, 2);

entries[0] = GTK_ENTRY (EntryIn);
entries[1] = GTK_ENTRY (EntryOut);

Or you can use one of the array containers inside GLib, like GPtrArray:

GPtrArray *entries = g_ptr_array_new ();

g_ptr_array_add (entries, EntryIn);
g_ptr_array_add (entries, EntryOut);

and then either use the pdata field to access the entry, or the convenience g_ptr_array_index() macro:

GPtrArray *entries = user_data;
GtkEntry *entryIn = entries->pdata[0]; // or g_ptr_array_index (entries, 0)
GtkEntry *entryOut = entries->pdata[1]; // or g_ptr_array_index (entries, 1)
1 Like

It works! Thank you! By the way, what do you think about these ways, which one is better?

That’s not true on various counts.

The GTK_ENTRY macro is still there; there’s no need to use a GtkEntryBuffer: a GtkEntry implements the GtkEditable interface, which has a gtk_editable_get_text() method.

If you’re porting from GTK3 to GTK4, you must read the porting guide, otherwise you’re going to have a bad time.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.