Re: How to update both the Console and the GTKMM gui window after running other functions



Thanks, Moh. I had lots of redundancy and other "most likely useless" arguments included just to make my original code compile. I didn't have a clear understanding about what was doing what. I was looking at the examples and trying to implement the objective of outputting to both the console and gtkmm.

I appreciate all the cleanup that Alan provided, plus a working model. At present, if it isn't too much of a distraction to the maillist, I appreciate any cleanup advice, or breakdown of which components are doing what.

It would be nice if it were stripped down to the bare minimum to achieve the objective, which might make it easier for me to understand the various components.

At present this is what I have:

// code begin
// --------------------------------------------
#include <gtkmm.h>
#include <iostream>

using namespace std;

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

protected:
    Gtk::Label m_label;
    string labeltext;
    string newtext;
    void myprocess1();
};

myLabel::myLabel() :
        m_label()
{
    void myprocess1();

    set_title("Gtkmm Programming - C++");
    add(m_label);

    m_label.show();

    Glib::Thread::create(sigc::mem_fun(*this, &myLabel::myprocess1), true);
}

myLabel::~myLabel()
{
}

void myLabel::myprocess1()
{
    labeltext = "About to preform a number of processes.\n";
    labeltext += "Each process may take up to three hours.\n";
    labeltext += "Please carry your daily chores and wait.\n";
    cout << labeltext;
    cout.flush();
    m_label.set_text(labeltext);

    sleep(10); // Back from a three hour function
    newtext = "Back from a three hour function\n";
    labeltext += newtext;
    m_label.set_text(labeltext);
    cout << newtext;
    cout.flush();

    sleep(10); // Back from a three hour function
    newtext = "Back from another three hour function\n";
    labeltext += newtext;
    m_label.set_text(labeltext);
    cout << newtext;
    cout.flush();

    newtext = "Exiting in 1 minute...\n";
    labeltext += newtext;
    m_label.set_text(labeltext);
    cout << newtext;
    cout.flush();
    sleep(60);
    exit(0);
}

int main(int argc, char* argv[])
{
    if (Glib::thread_supported())
        Glib::thread_init();
    else
    {
        cerr << "Threads aren't supported!" << endl;
        exit(1);
    }

    Gtk::Main kit(argc, argv);

    myLabel mylabel;
    Gtk::Main::run(mylabel);
    return 0;
}
// --------------------------------------------
// code end

I had a few parameters ("Hello World") because I thought something had to be there (as per the example I was looking at, at the time. But it appears that a lot of the "methods/functions" can just be open and closed parentheses, with nothing in it.

I don't know which parts might cause problems down the line, such as buffer overflows or memory leaks.

Also, as you see I added and "exit". I don't know if that will cause a problem, or if more cleanup should be done before exiting. I'm sure a lot of cleanup is performed automatically by clicking on the x to exit the gui window.

By the way, I was hoping to have something similar to:

Redirect cout to a TextView:
https://mail.gnome.org/archives/gtkmm-list/2007-May/msg00103.html

Not an actually cout redirection, but more less a "gprint()" function that would output to the gui window similar to a "cout" outputting to the console.

Since I have this current working model to be able to update the gui window, I can use it in my current applications, while continuing to study on the side how to make my "gprint()" function.

-- L. James

--
L. D. James
ljames apollo3 com
www.apollo3.com/~ljames

On 08/05/2013 09:19 PM, Moh B. wrote:
And another thing:
You declared a void myprocess1(void); member function <== WITH void
parameter list BUT in the myLabel::myLabel() constructor you called the
myprocess1() WITHOUT any parameter list: This may cause problems to the
compiler:
        protected:
        Gtk::Label m_label;
        string labeltext;
        string newtext;
        void myprocess1(); <== see here
caution required.
Good luck.
====================
On Mon, 2013-08-05 at 17:17 -0400, L. D. James wrote:
Thanks, Moh.  That resolved the error issue.  As far as I can see,
it's functioning perfect!

I'll have some questions on Threading.  But I'll reserve this for a
new topic.

-- L. James





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