#include <gtk/gtk.h>
#include <stdio.h>
typedef struct {
const gchar *host;
} example;
void b_clicked (GtkButton *c_button, example *test){
g_print("Hostname: %s\n", test->host);
}
int main (int argc, char *argv[]){
GtkWidget *window;
GtkWidget *grid;
GtkWidget *c_button;
GtkWidget *q_button;
GtkWidget *label_host;
GtkWidget *h_name;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "FTP Client");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
grid = gtk_grid_new();
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_grid_set_row_spacing (GTK_GRID (grid), 3);
label_host = gtk_label_new("Hostname");
example test;
h_name = gtk_entry_new();
test.host = gtk_entry_get_text(GTK_ENTRY (h_name));
gtk_entry_set_placeholder_text (GTK_ENTRY (h_name), "Hostname");
c_button = gtk_button_new_with_label ("Connect");
g_signal_connect (c_button, "clicked", G_CALLBACK (b_clicked), (gpointer*)&test);
q_button = gtk_button_new_with_label ("Quit");
g_signal_connect (q_button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), c_button, 0, 3, 2, 1);
gtk_grid_attach (GTK_GRID (grid), q_button, 0, 4, 2, 1);
gtk_widget_show_all (window);
gtk_main();
return 0;
}
這是什麼問題?
我沒有錯誤,沒有警告,但在終端上,該程序不能寫什麼:(
如果我寫:
gtk回調多個參數
test.host="trying something"
它的工作原理但gtk_entry_get_text它並沒有顯示什麼:(
我不明白...爲什麼它不適用於gtk_entry_get_text?
這是真的,你不能在C中做到這一點。這個問題已經在這裏解答:http://stackoverflow.com/questions/6684466/c-passing-multiple-arguments-to-a-callback-function- in-gtk – ptomato 2012-04-23 11:40:17
好的謝謝...所以我必須使用typedef結構?這是更好的方法? – polslinux 2012-04-23 12:01:16
是的,如果你有多個值傳遞給你的回調函數,那麼使用指向struct的指針作爲g_signal_connect的最後一個參數。您回調的原型必須始終遵循相應信號的原型。這裏是「[clicked]」(http://developer.gnome.org/gtk3/stable/GtkButton.html#GtkButton-clicked)「信號。 – liberforce 2012-04-23 12:24:58