Gtkmm3 Progress Bar - how to loop, fill, reset to 0

Hi everyone,

I have a for loop that should trigger progress bar set_fraction at each iteration.

for(int i = 0; i!=size; i++){
    if(m_bActivityMode)
    m_ProgressBar.pulse();
  else
  {
    double new_val = m_ProgressBar.get_fraction() + 1.0 / size;
    if(new_val > 1.0)
      new_val = 0.0;
    m_ProgressBar.set_fraction(new_val);
  }
}

I want to fill the bar to %100 at the end of the loop and then reset the progress bar to %0. What should I do ?

The question is unclear (even after fixing the title). What does this code do when compiled and run? Why is that wrong?

Do you mean to update the progress periodically (every N seconds, etc.)? Then you probably want to look into Glib::signal_timeout() to schedule periodic updates of the fraction. Just setting it to all the new values instantaneously of course won’t work

1 Like

new_val should not depend on the current value of the progress bar, you should likely just compute it as (i + 1) / size.

Then, as @dboles suggested, just calling set_fraction() won’t work as you want: GTK will not be able to actually draw your updates while you’re in the loop. Usually you’d perform the expansive operation in a thread, and schedule UI updates using g_idle_add() or similar. Another design is scheduling small portions of the work through the main loop, and only iterate once (or a couple times) at a time (e.g., have your work done in an idle handler, which performs only a few iterations, and remove that handler when the work is done).
And no, manually forcing updates is never a good design, and rarely the solution you’re looking for.

2 Likes

Thank you, now the issue is resolved !

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.