GTK Retrieve values from multiple widgets on button press

巧了我就是萌 提交于 2020-03-25 16:50:20

问题


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);

}

Button:

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

Any help would be greatly appreciated, thanks.


回答1:


The manual tells us that the signal handler for "clicked" signal must follow this signature:

void
user_function (GtkButton *button,
               gpointer   user_data)

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.

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


来源:https://stackoverflow.com/questions/60656483/gtk-retrieve-values-from-multiple-widgets-on-button-press

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!