GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error with GtkEntry

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming
Author Message
Chrishas



Joined: 09 May 2008
Posts: 2

PostPosted: Fri May 09, 2008 11:19 am    Post subject: Error with GtkEntry Reply with quote

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: 407
Location: Portland, OR USA

PostPosted: Fri May 09, 2008 3:59 pm    Post subject: gtk_entry_get_text Reply with quote

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



Joined: 09 May 2008
Posts: 2

PostPosted: Sat May 10, 2008 10:31 am    Post subject: Reply with quote

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: 407
Location: Portland, OR USA

PostPosted: Sat May 10, 2008 3:29 pm    Post subject: Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Programming All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP