开发者

Update multiple progress bar with gtk c++

I need to output the i progress bars and update them all. But only the last one updates i times. This is the code:

static void calculaPi (GtkButton * boton,  Datos * dDatos){
    const char * threads;
    GtkWidget * barra, *bot2, *button, *progress, *vbox;
    threads = gtk_entry_get_text(GTK_ENTRY(dDatos->dthreads ));
    gint ithreads  = 1;
    ithreads = atoi(threads);
    barra = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title((GtkWindow *) barra, "Loteria de Threads");
    gtk_window_set_default_size(GTK_WINDOW(barra), 300, ithreads*30);
    gtk_window_set_position(GTK_WINDOW(barra), GTK_WIN_POS_CENTER);
    button = gtk_button_new_with_label ("Click me!");
    vbox = gtk_vbox_new (FALSE, 5);
    gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 5);
    gtk_container_add (GTK_CONTAINER (barra), vbox);  
    for (gint i = 1 ; i <=  ithreads; i++) {  
       progress = gtk_progress_bar_new ();
       gtk_box_pack_start (GTK_BOX (vbox), progress, FALSE, FALSE, 5);
     开发者_StackOverflow  g_object_set_data (G_OBJECT (barra), "pbar", (gpointer) progress);
       g_signal_connect (G_OBJECT (button), "clicked",
                   G_CALLBACK (button_clicked), (gpointer) barra);
    }
    bot2 = gtk_button_new_with_label("Salir");
    gtk_box_pack_start (GTK_BOX (vbox), bot2, FALSE, FALSE, 5);
    gtk_widget_set_size_request(bot2, 100, 35);
    g_signal_connect (G_OBJECT (bot2), "clicked",
    G_CALLBACK (destroy),
    G_OBJECT (barra));
        gtk_widget_show_all(barra);
    gtk_main();
}
static void
button_clicked (GtkButton *button,
                GtkWidget *barra)
{
  GtkProgressBar *progress;
  gdouble percent = 0.0;
  gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE);
  progress = GTK_PROGRESS_BAR (g_object_get_data (G_OBJECT (barra), "pbar"));
  while (percent <= 100.0)
  {
    gchar *message = g_strdup_printf ("%.0f%% Complete", percent);
    gtk_progress_bar_set_fraction (progress, percent / 100.0);
    gtk_progress_bar_set_text (progress, message);

    while (gtk_events_pending ())
      gtk_main_iteration ();

    g_usleep (500000);
    percent += 5.0;
  }

}


With this line:

g_object_set_data (G_OBJECT (barra), "pbar", (gpointer) progress);

you override previous value of "pbar" data entry on each loop iteration. When you later retrive it in button_clicked() you get the last set value, i.e. the last progress bar.

In this particular case you can just pass progress widget as user data (and drop g_object_[gs]et_data() calls) for button_clicked() callback: the function doesn't use current barra window for anything else anyway.

In a more general way, you should learn how to use your own structures for user data parameters. A common way is to declare and use one structure for given toplevel type and store pointers to widgets you need to access from callbacks in it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜