Re: invalid cast from 'GtkButton' to 'GtkEntry'



When you press enter in the text entry, btn_clicked is called with the widget parameter set to the entry. When you press the button, the same function gets called, but widget is set to the button.

I see you pass the entry widget as the callback data when connecting to the signals. This means that in both cases the data parameter will contain the entry widget.

Knowing this, you may want to modify your callback to cast data into a GtkEntry (GtkEntry *entry = GTK_ENTRY(data)) and use the entry variable instead of widget.

Best,
Gergely


On Fri, Jan 20, 2017, 13:14 Michi.B <mail michael-buzgaru de> wrote:
I wrote a code in GTK3 and works fine:
<http://gtk.10911.n7.nabble.com/file/n90800/myApp-Entry.png>

And I get:

invalid cast from 'GtkButton' to 'GtkEntry'


And I can't figure out where the problem is.This happens After the User type
something and hits the Button.

Here is the Code:
#include <gtk/gtk.h>

static void btn_clicked(GtkWidget *widget, gpointer data)
{
        g_print("%s\n", gtk_entry_get_text(GTK_ENTRY(data)));
        gtk_editable_select_region(GTK_EDITABLE(data), 0,-1);
        gtk_editable_copy_clipboard(GTK_EDITABLE(data));
}

int main(int argc, char *argv[])
{
    GtkWidget *window, *grid;
    GtkWidget *label, *label_fn;
    GtkWidget *fn_entry;
    GtkWidget *button;

            //---------- CSS -------------
    GtkCssProvider *provider;
    GdkDisplay *display;
    GdkScreen *screen;
    //---------------------------

    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "MyApp");
    gtk_window_set_default_size(GTK_WINDOW(window), 370, 155);
    gtk_window_set_resizable (GTK_WINDOW(window), TRUE);
    gtk_container_set_border_width(GTK_CONTAINER(window), 5);
    g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

    /*     Create a Grid     */
    grid = gtk_grid_new();
    gtk_container_set_border_width(GTK_CONTAINER (grid), 15);
    gtk_widget_set_name(grid, "myGrid");
    gtk_grid_set_column_spacing(GTK_GRID(grid), 5);
    gtk_grid_set_row_spacing(GTK_GRID(grid), 5);
    gtk_container_add(GTK_CONTAINER(window), grid);

    /*     Create first Label     */
    label = gtk_label_new("Please enter your Information:");
    gtk_widget_set_margin_top(label, 25);
    gtk_widget_set_margin_start(label, 85);

    /*     Create second Label     */
    label_fn = gtk_label_new("First Name: ");
    gtk_widget_set_margin_start(label_fn, 10);

    /*     Create an Entry:     */
    fn_entry = gtk_entry_new();

    /*     Create a Button    */
    button = gtk_button_new_with_mnemonic("_Write text");
    g_signal_connect(button, "clicked", G_CALLBACK(btn_clicked), fn_entry);
    g_signal_connect(fn_entry, "activate", G_CALLBACK(btn_clicked),
fn_entry);


    /*     Putting all together      */
    gtk_grid_attach(GTK_GRID(grid), label,      0, 0, 2, 1);
    gtk_grid_attach(GTK_GRID(grid), label_fn,   0, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), fn_entry,   1, 1, 1, 1);
    gtk_grid_attach(GTK_GRID(grid), button,     1, 2, 1, 1);

        // ---------------------------------------------------- CSS
-----------------------------------------------------------
    provider = gtk_css_provider_new ();
    display = gdk_display_get_default ();
    screen = gdk_display_get_default_screen (display);
    gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER
(provider), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);

    const gchar *myCssFile = "mystyle.css";
    GError *error = 0;

    gtk_css_provider_load_from_file(provider,
g_file_new_for_path(myCssFile), &error);
    g_object_unref (provider);
    //
--------------------------------------------------------------------------------------------------------------------

    gtk_widget_show_all(window);
    gtk_main();
    return 0;
}

CSS File:

GtkWindow {
        background-color: magenta;
        color: black;
        border-width: 3px;
        border-color: blue;
}

#myGrid {
        background-color: red;
        border-style: solid;
        border-color: black;
        border-width: 3px;
        border-radius: 15px;
        border-color: grey;
}


And I compile it with the following:
gcc -Wall -Wextra -g myApp.c -o myApp `pkg-config --cflags --libs gtk+-3.0 `

After I type something and I hit the Button (Write text) I get the following
errors (warnings):


(myApp:10151): GLib-GObject-WARNING **: invalid cast from 'GtkButton' to
'GtkEntry'

(myApp:10151): Gtk-CRITICAL **: gtk_entry_get_text: assertion 'GTK_IS_ENTRY
(entry)' failed
(null)

(myApp:10151): GLib-GObject-WARNING **: invalid cast from 'GtkButton' to
'GtkEditable'

(myApp:10151): Gtk-CRITICAL **: gtk_editable_select_region: assertion
'GTK_IS_EDITABLE (editable)' failed

(myApp:10151): GLib-GObject-WARNING **: invalid cast from 'GtkButton' to
'GtkEditable'

(myApp:10151): Gtk-CRITICAL **: gtk_editable_copy_clipboard: assertion
'GTK_IS_EDITABLE (editable)' failed

Does Anyone know how to fix this?



--
View this message in context: http://gtk.10911.n7.nabble.com/invalid-cast-from-GtkButton-to-GtkEntry-tp90800.html
Sent from the Gtk+ - General mailing list archive at Nabble.com.
_______________________________________________
gtk-list mailing list
gtk-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-list


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]