简体   繁体   中英

Gtk2 Get data from 2 entries with the same button

So, I'm new to gtk and I'm trying to make a login form and I need username and password to be send both when button "Submit" is pressed. How can I do this? My code:

const void button_clicked (GtkWidget *widget,gpointer data)
{
    const gchar* text;
    text = gtk_entry_get_text(GTK_ENTRY(data));
    write(lala,text,100);
}
GtkWidget*window,*label_username,*label_password,*layout,*entry_username,*entry_password,*submit_button;
entry_username = gtk_entry_new();
entry_password = gtk_entry_new();
submit_button = gtk_button_new_with_label("Sumbit");
g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),entry_username;

How can I make the submit_button function to recive text from entry_username and entry_password? Thanks!

In gtk for situations like this it would be best to pass a structure which contains the widget's as members to the callback function...

typedef struct MainWindow 
{

GtkWidget *entry_username, *entry_password;

}MAINWINDOW;

int main(
MAINWINDOW *mainwindow = malloc(sizeof(MAINWINDOW));

mainwindow->entry_username = gtk_entry_new();
mainwindow->entry_password = gtk_entry_new();


g_signal_connect(submit_button,"clicked",G_CALLBACK(button_clicked),mainwindow);
}

const void button_clicked (GtkWidget *widget,gpointer data)
{
    MAINWINDOW *mainwindow = data;
    //now you have both the widget's 
        // modify em as per your wish :)
}

this is a very rough example. but something like this should work for your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM