GTK在按下按钮时从多个小部件中检索值 [英] GTK Retrieve values from multiple widgets on button press
问题描述
我试图在按下按钮时从我的窗口中的Entry字段和ComboBox两者中检索一个值.我目前正在努力做到这一点.有人告诉我,为了获得多个价值,我需要使用结构.但是,我正在努力使其正常工作.这是我到目前为止的内容:
I'm trying to retrieve a value from both an Entry field and a ComboBox in my window on a button press. I'm currently struggling to do so. I've been told that in order to get multiple value, I need to use structs. However I'm struggling to get it to work. Here is what I have so far:
要在按下按钮时运行的功能:
Function to be run on button press:
struct data {
GtkWidget *hash;
GtkWidget *hashType;
};
static void queue_hash (struct data *dataStruct) {
GtkWidget *hashWid = dataStruct->hash;
GtkWidget *hashTypeWid = dataStruct->hashType;
const char* hash = gtk_entry_get_text(GTK_ENTRY(hashWid));
char* hashType = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(hashTypeWid));
g_print ("Queue Hash: %s %s\n", hash, hashType);
}
按钮:
GtkWidget *hashEntry;
GtkWidget *hashSelect;
hashEntry = gtk_entry_new();
gtk_widget_set_size_request(hashEntry, 290, 33);
gtk_fixed_put(GTK_FIXED(window_fixed), hashEntry, 300, 75);
hashSelect = gtk_combo_box_text_new();
gtk_widget_set_size_request(hashSelect, 102, 25);
gtk_fixed_put(GTK_FIXED(window_fixed), hashSelect, 595, 75);
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hashSelect), "MD5");
gtk_combo_box_text_append_text(GTK_COMBO_BOX_TEXT(hashSelect), "SHA1");
queueButtonBox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
queueButton = gtk_button_new_with_label("Queue Hash");
gtk_fixed_put(GTK_FIXED(window_fixed), queueButtonBox, 300, 120);
struct data *cb_data = g_new0(struct data, 1);
cb_data->hash = hashEntry;
cb_data->hashType = hashSelect;
g_signal_connect (queueButton, "clicked", G_CALLBACK (queue_hash), cb_data);
运行代码时,出现此错误:
When I run the code, I get this error:
(SDS-CW:16982): GLib-GObject-WARNING **: 14:42:38.659: invalid uninstantiatable type 'void' in cast to 'GtkEntry'
(SDS-CW:16982): Gtk-CRITICAL **: 14:42:38.659: gtk_entry_get_text: assertion 'GTK_IS_ENTRY (entry)' failed
非常感谢您的帮助.
推荐答案
The manual tells us that the signal handler for "clicked"
signal must follow this signature:
void
user_function (GtkButton *button,
gpointer user_data)
您的函数仅需要1个参数,并且对该信号无效.结果,您使用button
指针并将其解释为将失败的结构.
You function only expects 1 parameter and is invalid for this signal. As a result you take the button
pointer and interpret it as your struct which will fail.
要解决此问题,请提供同时带有两个参数的签名,或者可以将信号处理程序与功能g_signal_connect_swapped
连接.
To solve this problem either provide a signature taking both parameters or you could connect the signal hander with function g_signal_connect_swapped
.
您可以像这样更改初始处理程序功能:
You could change your initial handler function like this:
static void queue_hash (GtkButton *button, gpointer user_data) {
struct data *dataStruct = user_data;
GtkWidget *hashWid = dataStruct->hash;
GtkWidget *hashTypeWid = dataStruct->hashType;
const char* hash = gtk_entry_get_text(GTK_ENTRY(hashWid));
char* hashType = gtk_combo_box_text_get_active_text(GTK_COMBO_BOX_TEXT(hashTypeWid));
g_print ("Queue Hash: %s %s\n", hash, hashType);
}
这篇关于GTK在按下按钮时从多个小部件中检索值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!