gtk回调多个参数

时间:2012-04-23 08:37:09

标签: gtk gtk3

#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;
}

这有什么问题? 我没有错误,没有警告,但在终端上这个程序没有写任何东西:(
如果我写:

test.host="trying something"

它有效,但是用gtk_entry_get_text它没有显示任何内容:(
我不明白......为什么它不适用于gtk_entry_get_text?

1 个答案:

答案 0 :(得分:3)

您需要了解GTK是一个事件驱动的工具包(与许多其他工具包一样)。您需要与事件互动。但是在你运行gtk_main之前它不会检查事件。所以你的问题是你正在使用test.host = gtk_entry_get_text(GTK_ENTRY (h_name))读取主机名,但那时,小部件尚未显示,你甚至没有在其中键入任何内容!所以你基本上只是从这里得到一个空字符串,这就是你单击“连接”按钮时显示的内容。

实现此目的的一种方法是将指针指向结构中的小部件。这样,您可以从b_clicked回调中调用gtk_entry_get_text。这样,您获得的值就是当时文本输入小部件中的值

#include <gtk/gtk.h>
#include <stdio.h>

typedef struct {
        GtkWidget *host;
} example;

void b_clicked (GtkButton *c_button, example *test){
        g_print("Hostname: %s\n", gtk_entry_get_text (GTK_ENTRY(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 = 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), &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;
}

另一个更好的方法是,在不修改结构的情况下,要求在文本发生变化时收到通知。为此,使用“changed”信号,因为GtkEntry实现了GtkEditable接口。请参阅“GtkEntry text change signal”。

请注意(gpointer*)&test错误。 test是一个struct,&amp; test是一个struct的地址。 gpointer是一个空洞*,即。已经是一个指针,所以gpointer *是一个指向指针的指针,这不是什么&amp; test。所以只需写下&test