signal_timeout vs loops (changing label text continuously)



Hi all.

Luis Federico in the "displaying text" thread asks how can he print some
text to the screen on each loop iteration.

I though I should try to help him but I found myself in trouble. I
thought that having a Label and just changing it's text with the
set_text() method would do the job. However this doesn't really work,
and I was wondering if someone could help me understand why the label
changes only after the last loop iteration completes. It seems that when
the loop is executing the window doesn't redraw itself at all. What
gives?

Thanks,
-Gezim
#include <gtkmm.h>
#include <sstream>
#include <iostream>
#include <unistd.h>

class MyWindow: public Gtk::Window
{
    public:
    MyWindow ();
    virtual ~MyWindow ();

    void on_button1_clicked ();
    void loop ();

    Gtk::Label showIterLabel;
    Gtk::Button button1;
    Gtk::HBox boxy;
    int i;
};

int main (int argc, char * argv[])
{
    Gtk::Main kit (argc, argv);

    MyWindow window;

    kit.run (window);

    return 0;
}

MyWindow::MyWindow ()
    : button1("Start"),
    i(0)
{
    set_default_size (400, 400);
    add (boxy);
    showIterLabel.set_text("stopped");

    boxy.pack_start (showIterLabel);
    boxy.pack_start (button1);

    button1.signal_clicked ().connect (sigc::mem_fun (*this, &MyWindow::loop) );
    show_all_children();
}

MyWindow::~MyWindow ()
{}

void MyWindow::loop ()
{
    std::ostringstream stream1;

    while (i < 5)
    {
        stream1.str("");
        stream1 << i;
        std::cout << i << std::endl;
        showIterLabel.set_text (stream1.str());
        i++;
        sleep (1);
    }
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]