How do I pass multiple variables as data with gtk signals

后端 未结 3 1909
再見小時候
再見小時候 2021-02-14 02:01

I have a small program where a gtk signal callback function needs 2 or 3 variables.

I don\'t want to make these global variables (The entire goal of the project is to be

3条回答
  •  面向向阳花
    2021-02-14 02:39

    Of course you can use arrays of void pointers, but if you want to pass around values with different types (especially values whose type is longer than sizeof(void *)), you can't use arrays. For these, you'll almost certainly want to wrap them in a struct and pass the struct's address as the data parameter.

    Example:

    struct my_struct *data = malloc(sizeof(*data));
    data->field_one = value_one;
    data->field_two = value_two; /* etc. */
    
    g_signal_connect(save, "clicked", callback, data);
    

    Of course, don't forget to free(data) in the callback function (assuming it's for single use).

    Edit: as you wanted an example with void **, here you are (this is ugly, and I don't recommend you to use this -- either because allocating a one-element array for primitive types is wasting your shoot or because casting a non-pointer to void * is bad practice...):

    void **data = malloc(sizeof(data[0]) * n_elements);
    
    type1 *element1_ptr = malloc(sizeof(first_item));
    *element1_ptr = first_item;
    data[0] = element1_ptr;
    
    /* etc. */
    

    To free them:

    int i;
    for (i = 0; i < n_elements; i++)
        free(data[i]);
    
    free(data);
    

提交回复
热议问题