GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Oct 25, 2014 9:13 pm

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: Error with GtkEntry
PostPosted: Fri May 09, 2008 11:19 am 
Offline
Familiar Face

Joined: Fri May 09, 2008 11:12 am
Posts: 5
I have the following function in C:

Quote:
static void check_id(GtkWidget *text[]){
GtkWidget *username;
GtkWidget *password;
username = gtk_entry_new();
password = gtk_entry_new();
username = text[0];
password = text[1];
char* identification[2];

//printf("%s", gtk_entry_get_text(GTK_ENTRY (password)));
identification[0] =(char*)gtk_entry_get_text(GTK_ENTRY (password));
//printf("%s", identification[0]);
//identification[1] = gtk_entry_get_text(GTK_ENTRY (text[1]));

}


and I receive a segmentation fault when I try to assign the text of password to identification[0]. Can anyone help?

Thx in advance,
Chrishas


Top
 Profile  
 
 Post subject: gtk_entry_get_text
PostPosted: Fri May 09, 2008 3:59 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
GtkWidget *username;
GtkWidget *password;
username = gtk_entry_new();
password = gtk_entry_new();
username = text[0];
password = text[1];

You're saying password is a pointer to a GtkWidget, assigning it to the return value of gtk_entry_new () and then reassigning it to text[1] which is passed to the function, presumptively an array.

So there are lots of issues here. I would try this line of code:
GTK_IS_ENTRY(password)
And I'll think you'll find that it is not an entry.

Can you paste the code that calls this function?

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 10, 2008 10:31 am 
Offline
Familiar Face

Joined: Fri May 09, 2008 11:12 am
Posts: 5
Here's the code:

Quote:
static void login()
{
GtkWidget *login_window;
GtkWidget *user_pass_text[2];
//GtkWidget *pass_text;
GtkWidget *login_button;
GtkWidget *fixed;
GtkWidget *luser;
GtkWidget *lpass;


/* Create login window */
login_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_set_border_width (GTK_CONTAINER (login_window), 10);
gtk_window_set_skip_taskbar_hint(GTK_WINDOW (login_window), TRUE);

/* Signal handling for close button */
g_signal_connect (G_OBJECT (login_window), "destroy",
G_CALLBACK (gtk_widget_destroy), G_OBJECT(login_window));

/* Create new fixed container */
fixed = gtk_fixed_new();

/* Add fixed container to window container */
login_button = gtk_button_new_with_label ("Login");
luser = gtk_label_new("Username");
lpass = gtk_label_new("Password");

user_pass_text[0] = gtk_entry_new();
user_pass_text[1] = gtk_entry_new();

g_signal_connect (G_OBJECT (login_button), "clicked",
G_CALLBACK (check_id), &user_pass_text);

gtk_entry_set_visibility(GTK_ENTRY (user_pass_text[1]),FALSE);

// Add widgets to their containers
gtk_container_add(GTK_CONTAINER(login_window), GTK_WIDGET(fixed));
gtk_fixed_put(GTK_FIXED(fixed), luser,1,1);
gtk_fixed_put(GTK_FIXED(fixed), lpass,1,40);
gtk_fixed_put(GTK_FIXED(fixed), user_pass_text[0],100,1);
gtk_fixed_put(GTK_FIXED(fixed), user_pass_text[1],100,40);
gtk_fixed_put(GTK_FIXED(fixed), login_button,100,80);

// Display Widgets
gtk_widget_show(fixed);
gtk_widget_show(luser);
gtk_widget_show(lpass);
gtk_widget_show(user_pass_text[0]);
gtk_widget_show(user_pass_text[1]);
gtk_widget_show(login_button);
gtk_widget_show(login_window);
gtk_grab_add(login_window);
}


I've only been programming with gtk for a few days so I realise my coding perhaps sucks.:p. There is also the problem of taking both the username and password, as I was using a callback function to check them I was unable to pass more than one argument to that function thus I put them in an array and passed it as a parameter.


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 10, 2008 3:29 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
We typically use structs to hold our widget references when we need to pass more than one to a callback function as user_data. This example uses that method to pass and entry and a textview to a callback function: http://www.gtkforums.com/about906.html

It makes the code nice and easy to read and is a step closer to creating GObjects which you might pass around to callbacks.


Code:
typedef struct
{       
        GtkWidget               *user_name;
        GtkWidget               *password;
} MyWidgets;

static void login()
{
        GtkWidget *login_window;
        GtkWidget *login_button;
        GtkWidget *fixed;
        GtkWidget *luser;
        GtkWidget *lpass;
        MyWidgets *widgets;

        /* allocate space for MyWidgets struct */
        widgets = g_slice_new (MyWidgets);
       
        /* Create login window */
        login_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width (GTK_CONTAINER (login_window), 10);
        gtk_window_set_skip_taskbar_hint(GTK_WINDOW (login_window), TRUE);

        /* Signal handling for close button */
        g_signal_connect (G_OBJECT (login_window), "destroy",
        G_CALLBACK (gtk_widget_destroy), G_OBJECT(login_window));

        /* Create new fixed container */
        fixed = gtk_fixed_new();

        /* Add fixed container to window container */
        login_button = gtk_button_new_with_label ("Login");
        luser = gtk_label_new("Username");
        lpass = gtk_label_new("Password");

        widgets->user_name = gtk_entry_new();
        widgets->password = gtk_entry_new();

        g_signal_connect (G_OBJECT (login_button), "clicked",
        G_CALLBACK (check_id), widgets);

        gtk_entry_set_visibility(GTK_ENTRY (widgets->password),FALSE);

        // Add widgets to their containers
        gtk_container_add(GTK_CONTAINER(login_window), GTK_WIDGET(fixed));
        gtk_fixed_put(GTK_FIXED(fixed), luser,1,1);
        gtk_fixed_put(GTK_FIXED(fixed), lpass,1,40);
        gtk_fixed_put(GTK_FIXED(fixed), widgets->user_name,100,1);
        gtk_fixed_put(GTK_FIXED(fixed), widgets->password,100,40);
        gtk_fixed_put(GTK_FIXED(fixed), login_button,100,80);

        // Display Widgets
        gtk_widget_show(fixed);
        gtk_widget_show(luser);
        gtk_widget_show(lpass);
        gtk_widget_show(widgets->user_name);
        gtk_widget_show(widgets->password);
        gtk_widget_show(login_button);
        gtk_widget_show(login_window);
        gtk_grab_add(login_window);
}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 8:15 am 
Offline
Familiar Face

Joined: Fri May 09, 2008 11:12 am
Posts: 5
Thanks for the struct tip, that helps simplify the code, but the problem still persists with trying to get the text from the entry. Here is the error:

Quote:
(gui:11559): GLib-GObject-WARNING **: invalid uninstantiatable type `<invalid>' in cast to `GtkEntry'

(gui:11559): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed

(gui:11559): GLib-GObject-WARNING **: invalid uninstantiatable type `<invalid>' in cast to `GtkEntry'

(gui:11559): Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed


and here's the function that is called that causes it:
Quote:
static void check_id(MyWidgets *widgets){
GtkWidget *username;
GtkWidget *password;
username = gtk_entry_new();
password = gtk_entry_new();
username = widgets->user_name;
password = widgets->password;
char* identification[2];

GtkWidget *dialog;

/* create an error dialog containing the error message */
dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
gtk_entry_get_text(GTK_ENTRY (widgets->user_name)));
gtk_window_set_title (GTK_WINDOW (dialog), "Error");

/* run the dialog */
gtk_dialog_run (GTK_DIALOG (dialog));

/* clean up memory used by dialog and error */
gtk_widget_destroy (dialog);

printf("%s", gtk_entry_get_text(GTK_ENTRY (username)));
//identification[0] =(char*)gtk_entry_get_text(GTK_ENTRY (password));
//printf("%s", identification[0]);
//identification[1] = gtk_entry_get_text(GTK_ENTRY (text[1]));

}


Top
 Profile  
 
 Post subject: Your handler function's formate is wrong.
PostPosted: Mon May 12, 2008 9:55 am 
Offline
Familiar Face

Joined: Mon May 05, 2008 3:37 am
Posts: 10
It should be
void click_id(GtkWidget *widget, MyWidgets *widgets)


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 9:59 am 
Offline
Familiar Face

Joined: Fri May 09, 2008 11:12 am
Posts: 5
But it's a callback function, I can only pass one argument to it, unless you meant making it non static which doesn't really affect anything essential


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 1:36 pm 
Offline
Familiar Face

Joined: Mon May 05, 2008 3:37 am
Posts: 10
Please replace your
click_id(MyWidget *widgets)
with
void click_id(GtkWidget *widget, MyWidgets *widgets)

the first para of the callback func is pass by default.this is difinde by the
button's "clicked" signal. The first para is reference to the button ,which
send the clicked signal.

static or not is does not matter here..


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 12, 2008 3:02 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
As luhuadeng pointed out, the first argument to any callback function is the object which triggered the signal.

When writing callback functions, first check the manual for that signal's callback prototype. In the case of "clicked", the API documentation shows this:

Code:
void                user_function                      (GtkButton *button,
                                                        gpointer   user_data)      : Run First / Action


This tells us that when writing our callback function for the clicked signal, we write a function returning void which accepts a GtkButton as the first parameter (we could do GtkWidget instead since a GtkButton is derived from GtkWidget) and the second parameter is our user data-- in this case, a MyWidgets struct. So it could end up looking like this:

Code:
void check_id (GtkButton *button, MyWidgets *widgets)



Also, looking at your code, you have the GtkEntry references in the MyWidget structure, yet you are also creating 2 new entries referenced by 'username' and 'password'. So, the printf towards the bottom will be getting the text of an empty, newly created entry. I dont' think that's what you intended... is it?

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group