我有以下声明:
typedef struct window_and_search_entry
{
GtkWidget *window;
GtkWidget *search_entry;
} WINDOW_AND_SEARCH_ENTRY;
and then in main:
GtkWidget *window;
GtkWidget *search_entry;
...
WINDOW_AND_SEARCH_ENTRY window_and_search_entry;
window_and_search_entry.window = window;
window_and_search_entry.search_entry = search_entry;
还有那两个:
g_signal_connect_swapped(G_OBJECT(search_entry), "activate", G_CALLBACK(analyse), (gpointer) &window_and_search_entry);
g_signal_connect_swapped(G_OBJECT(do_it_button), "clicked", G_CALLBACK(analyse), (gpointer) &window_and_search_entry);
我想创建接受文本和窗口的函数,对其进行一些操作,如果发生错误,则使用其他接受窗口作为参数的函数将其打印出来
ELEMENT *analyse(GtkWidget *widget, gpointer user_data)
{
//((WINDOW_AND_SEARCH_ENTRY *) user_data)->search_entry;
GtkWidget *a = ((WINDOW_AND_SEARCH_ENTRY*)(user_data))->search_entry;
const char *text = gtk_entry_get_text(GTK_ENTRY(a));
g_print("%s\n", text);
ELEMENT *heap[100];
int index = 0;
return heap[1];
}
我尝试了很多变体,但在 entry_box 中输入内容后按回车或按钮后出现“段错误”。我想将文本打印到控制台。请帮助我,谢谢。
您的analyse
回调需要g_signal_connect
,而不是g_signal_connect_swapped
。也不需要转换指针:G_CALLBACK()
宏已经转换了整个函数,所以这就足够了:
ELEMENT *analyse(GtkWidget *widget, WINDOW_AND_SEARCH_ENTRY *data)
使用 g_signal_connect_swapped
它将是:
ELEMENT *analyse(WINDOW_AND_SEARCH_ENTRY *data, GtkWidget *widget);
这个交换版本只是对已有函数的 C 便利,例如你可以这样做:
g_signal_connect_swapped(G_OBJECT(search_entry), "activate",
G_CALLBACK(g_print), "Activate signal called");
我是一名优秀的程序员,十分优秀!