 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
Chrishas Familiar Face
Joined: 09 May 2008 Posts: 5
|
Posted: Fri May 09, 2008 11:19 am Post subject: Error with GtkEntry |
|
|
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 |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 497 Location: Portland, OR USA
|
Posted: Fri May 09, 2008 3:59 pm Post subject: gtk_entry_get_text |
|
|
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? |
|
| Back to top |
|
 |
Chrishas Familiar Face
Joined: 09 May 2008 Posts: 5
|
Posted: Sat May 10, 2008 10:31 am Post subject: |
|
|
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. |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 497 Location: Portland, OR USA
|
Posted: Sat May 10, 2008 3:29 pm Post subject: |
|
|
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: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| 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);
} | |
|
| Back to top |
|
 |
Chrishas Familiar Face
Joined: 09 May 2008 Posts: 5
|
Posted: Mon May 12, 2008 8:15 am Post subject: |
|
|
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]));
}
|
|
|
| Back to top |
|
 |
luhuadeng Familiar Face
Joined: 05 May 2008 Posts: 10
|
Posted: Mon May 12, 2008 9:55 am Post subject: Your handler function's formate is wrong. |
|
|
It should be
void click_id(GtkWidget *widget, MyWidgets *widgets) |
|
| Back to top |
|
 |
Chrishas Familiar Face
Joined: 09 May 2008 Posts: 5
|
Posted: Mon May 12, 2008 9:59 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
luhuadeng Familiar Face
Joined: 05 May 2008 Posts: 10
|
Posted: Mon May 12, 2008 1:36 pm Post subject: |
|
|
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.. |
|
| Back to top |
|
 |
Micah Carrick Never Seen the Sunlight
Joined: 21 Sep 2005 Posts: 497 Location: Portland, OR USA
|
Posted: Mon May 12, 2008 3:02 pm Post subject: |
|
|
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: (C) | 1 2
| 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: (C) | 1
| 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? |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|