Alireza Alipour asl
Alireza Alipour asl

Reputation: 11

How insert a progress bar for showing run time progress a project?

My project has a run time. I show it with below code:

#include <opencv2/opencv.hpp>
#include <gtk/gtk.h>
#include <time.h>

using namespace cv;
using namespace std;

gboolean Func (gpointer data) {
    gdouble value;
    GString *text;

    //part of code that generate run time
    clock_t start, end;
    start = clock();

    Mat image, src1, DENO;
    image = imread("C:/Users/Alireza/Desktop/1.png");
    cvtColor(image, src1, CV_RGB2GRAY);
    fastNlMeansDenoising(src1, DENO, 19, 29, 38);
    imwrite("DENO.png", DENO);

    end = clock();
    int runtime = ((int)(end - start)) / CLOCKS_PER_SEC;
    cout << "Time1 = " << runtime << "s" << endl;


    //part of code that I want to show run time progress by progress bar
    value = gtk_progress_bar_get_fraction(GTK_PROGRESS_BAR(data));

    value += 0.01;
    if (value > 1.0) {
        value = 0.0;
    }

    text = g_string_new(gtk_progress_bar_get_text(GTK_PROGRESS_BAR(data)));
    g_string_sprintf(text, "%d%%", (int)(value * 100));
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(data), value);
    gtk_progress_bar_set_show_text(GTK_PROGRESS_BAR(data), TRUE);
    gtk_progress_bar_set_text(GTK_PROGRESS_BAR(data), text->str);

    while (gtk_events_pending())
        gtk_main_iteration();

    return TRUE;
}

int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *progressBar;
gint timer;

gtk_init(&argc, &argv);

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "GtkProgressBar");
gtk_window_set_default_size(GTK_WINDOW(window), 300, 30);

progressBar = gtk_progress_bar_new();
timer = g_timeout_add(100, Func, progressBar);

gtk_container_add(GTK_CONTAINER(window), progressBar);

gtk_widget_show_all(window);

gtk_main();

return 0;

}

I want show progress my project using progress bar. Image is:

1.png

But I can't combine these codes together to show progress run time of my project. I'm new in gtk3. Does any one have any thoughts or suggestions on this?

Upvotes: 0

Views: 409

Answers (1)

jcoppens
jcoppens

Reputation: 5440

I think there are several other issues in your program. About the progressbar:

When calling gtk_progress_bar_set_fraction you actually do update the progress bar, but you don't give Gtk time to do the drawing of the updated bar. (Also, the call to gtk_progress_bar_set_fraction should be inside the for-loop)

So, in order to allow Gtk to do that (and also attend to other events, such as clicks and keyboard), you have to call a function which allows the Gtk main loop to attend to the pending requests.

Have a look at the functions gtk_main_iteration, gtk_main_iteration_do. You'll have to include one those in your math loop.

Upvotes: 1

Related Questions