Re: Gtk::ProgressBar is not being updated inside a for statement
- From: Robert Pearce <rob bdt-home demon co uk>
- To: gtkmm-list gnome org
- Subject: Re: Gtk::ProgressBar is not being updated inside a for statement
- Date: Fri, 11 Sep 2009 08:36:00 +0100
On Fri, 11 Sep 2009 02:08:24 +0300 "sledge" wrote:
>
> > void wait(double seconds){
> > clock_t end_wait;
> > end_wait = clock () + end_wait * CLOCKS_PER_SEC ;
> > while (clock() < end_wait) {}
> > }
> >
> > for(int i=1; i<=max_time; i++){
> >
> > wait(1); //Waits one second
> >
> > myProgressBar.set_fraction(i/max_time); //it updates the
> > GUI until it has returned to the main window loop
> >
> > while(Gtk::Main::events_pending()) //this should update
> > the progress bar in GUI on each iteration in this for statement, but it
> > doesn't
> >
> > Gtk::Main::iteration();//I tried both arguments, false
> > and true, none of them worked, same result... it gets updated when it
> > exits the function that uses this for statement
> > }
>
> You should put the Gtk::Main::iteration() inside the while loop:
>
> while(Gtk::Main::events_pending())
> {
> Gtk::Main::iteration();
> }
>
Looks like it already is inside the while - there's no semicolon on the line with the while. Granted the braces would make that clearer (which is why the "MISRA" guidelines for safety-critical C require them) or at the very least some indentation, but they're redundant in this case.
No, as somebody else has posted, the problem is
myProgressBar.set_fraction(i/max_time);
will only ever pass 0 or 1 because it's doing an integer divide. Change it to
myProgressBar.set_fraction ( (float)i / (float)max_time );
That said, stuffing a Gtk::Main::iteration(); call into your code is ALWAYS a sign of bad programming, even if it's the only solution you can find to a problem you've lumbered yourself with due to a bad architectural decision three years and two projects ago. The right way to do the job here is to start a timer event so the GTK main-loop will call your update function regularly. The update function won't contain a for loop, or a while loop, or any Gtk::Main:: method calls, and there won't be a function called wait either.
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]