简体   繁体   English

使用GTK回调函数提交数组

[英]Submit array with GTK callback function

I have a lot of check buttons (cbuttons[]) in my main function. 我的主要功能中有很多检查按钮(cbuttons [])。

After click of the install button, the array with the check buttons are commited. 单击安装按钮后,将提交带有检查按钮的阵列。

int main(int argc, char *argv[]) {

GtkWidget *cbuttons[13];

[...]

for(i = 0; i < 6; i++) {
    cbuttons[i] = gtk_check_button_new_with_label(cbuttons_label[i]);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cbuttons[i]), TRUE);
    gtk_box_pack_start(GTK_BOX(vbox_lcbuttons), cbuttons[i], FALSE, TRUE, 0);
    gtk_widget_show(cbuttons[i]);
 }

   [...]

   g_signal_connect(button_install, "clicked", G_CALLBACK(install), &cbuttons);

}

Now I want to analyse the state of the different buttons with data[] but it doesn't work: 现在,我想用data []分析不同按钮的状态,但是不起作用:

warning: dereferencing 'void *' pointer. 警告:取消引用“ void *”指针。

I'm new with GTK. 我是GTK的新手。 Maybe it is the wrong way to do this? 也许这是错误的方法吗? Can anybody help me to the right way? 谁能以正确的方式帮助我? (Sorry for my bad english. I'm not so familiar with the language.) (对不起,我的英语不好。我不太熟悉这种语言。)

void install(GtkWidget *widget, gpointer data) {

if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*data[0]))) {
    func1();
}
  if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*data[1]))) {
    func2();
  }
  [...]

}

You simply need to convert the generic ( gpointer , which is just a synonym for void * ) pointer to a properly typed one, in your callback: 您只需要在回调中将通用指针( gpointer ,它只是void *的同义词)转换为正确类型的指针:

static void install(GtkWidget *widget, gpointer data)
{
    GtkWidget **widgets = data;

    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets[0]))) {
      func1();
    }
    if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets[1]))) {
      func2();
    }
   [...]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM