gpt4 book ai didi

gtk 回调多个参数

转载 作者:行者123 更新时间:2023-12-04 18:22:58 24 4
gpt4 key购买 nike

#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?

最佳答案

您需要了解 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 接口(interface)。请参阅“ GtkEntry text change signal”。

另请注意 (gpointer*)&test是错的。 test 是一个结构体,&test 是一个结构体的地址。 gpointer 是一个 void *,即。已经是一个指针,所以 gpointer *是指向指针的指针,而 &test 不是。所以写 &test .

关于gtk 回调多个参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10277279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com