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



The sigc++ things aren't so hard to use once you understand them.
Basically all you have to do is to create a functor (an object representing a function) for your signal handler and pass it to the connect function of a signal (e.g. signal_show of any widget, signal_clicked of a button), then your signal handler will be called whenever the signal is emitted.
The signal handler is just a normal function or a member function of one of your classes. It has to have a certain prototype for a certain signal.
The before mentioned signal_show and signal_clicked are both of the type Glib::SignalProxy0<void> meaning your signal handler has to have the return type void and has to take no arguments. (technically this is only a restriction for the functor and not for your function but this is advanced stuff)
The prototypes needed for signal handlers are also shown in the documentation for every signal.

And now about how to actually do this. Here is an example:


void on_example_win_show();

int main()
{
    Gtk::Window example_win; // just an empty window
   
    main_win.signal_show().connect(sigc::ptr_fun(&on_example_win_show)); // connect the on_example_win_show function to signal_show
    /*
    The '&' is used to get a function pointer to on_example_win_show.
    sigc::ptr_fun is used to create functors for static methods and normal functions from a function pointer.
    For member functions, you would use sigc::mem_fun
    */
   
    Gtk::Main::run(example_win); // even though it will be deprecated, it works for now
    return 0;
}

void on_example_win_show()
{
    // execute some stuff right after the window is shown
}


Am 31.07.2013 14:50, schrieb L. D. James:
Thanks Jonas.  I have a basic understanding of what you're saying.  I kind of figured most of this before my post.  I just can't figure out how to actually do it.  I'm still searching and reading all the documenting that I can find to help me to figure out how to do it.  I have been reading so much documentation, I'm almost dizzy, but still reading.

It was about all I could do to describe what I'm trying to accomplish.  I believe I have come very far to get some type of definition where most of the comments are on the same track.  And it's very difficult taking in all the details.

Along with all the other studies, I'm studying signals.  I don't fully understand what I'm reading about them, but I'm studying them.  Once I figure out how to write one I'll then have to start studying where to put the signal.

I hope you can remember when you wrote your first hello world in your first programming language.  By the way I wrote my first hello world in basic.  Then I wrote my second hello world in assembly.  Trying to get a screen update in gtkmm (without examples) is reminding me of writing in assembly language.  There's so much to take in that I almost don't know where to start and which parts is the most pertinent and which parts does what.  Most of the comments are speaking of a different part to focus on.  Of course all the comments might be talking about the same part.  But to a novice in this.  They all seem different.  After each message I try to take in the gist and start looking for that perspective.

Once user sent a private message telling me what I'm trying to do is extremely complicated.

-- L. James

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

Being so new to this very unique way of interfacing with a gui window, I don't know which details of my studying is the most pertinent (without some type of example that works).

On Wed, 2013-07-31 at 14:19 +0200, Jonas Platte wrote:
Gtk::Main::run runs until the window is closed. If you want to let the window open while executing code that takes time to finish (like copying many files in an installation), you should create a seperate thread that does those things and updates the UI respectively.
For changing the label after some not time-intensive calculations that should be done right after the window is shown, you would only need to pack those into a function that you connect to signal_show of your window. You can find other signals that are emitted when certain events happen in the documentation.

Am 31.07.2013 13:57, schrieb L. D. James:

Thanks, Ian.  And yes.  I know that my code is performing outside the gui environment.  I gave the example so that the community would understand what I was trying to do and tell me what changes had to happen to have the code perform inside the gui environment.

I have already found examples that use the new version you mention.  I'm using that version now and still having the same problem.  This is because I don't know where to place the lines to perform output to the gui window.

I'm baffled that there isn't anyone in the community that has every sent new text to a gui window without the user having to press a key.  It's similar to the way Windows used to perform a fresh install.  You had to sit at the console and keep pressing keys to continue.  They finally went to a method to allow you to answer a few questions, then you can proceed with the install and look at the status of the update without having to sit there and press keys.

I'm glad, that from your description that you understand the objective and have put it in very professional and descriptive words.  Hopefully the developer will add and example to the elaborate code snippets of the documentation.

I'm investigating the use of sigc::timeout.  I find that the button widget uses it.  I'm trying to figure out how to bypass the button and perform the button function without pressing the button.

Thanks again for all your input and any other hints that might come to your mind of which I should look at.

-- L. James

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

On Wed, 2013-07-31 at 22:37 +1200, Ian Martin wrote:
Hi,
On review, you're using Gtk::Main::run(), which is in the process of getting deprecated, and probably shouldn't be used in new code unless you're working with a gtkmm version <3.0  The progress bar example in the book has the "new" version,
 Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");

  ExampleWindow window;

  //Shows the window and returns when it is closed.
  return app->run(window);
which you should probably be using instead.

Regarding the original question, your underlying issue is you're trying to modify information in isolation from the GUI environment, but want the GUI to respond.  The gtkmm window is created on the display with Gtk::Main::run(window) or app->run(window); in the code you started with, that's the last line of the main function, so any changes before that will happen before it becomes visible. 
The examples in the book all(?) contain at least 3 files: one brief and standardised for main.cpp, and two for the ExampleWindow cpp and h files, where all the work is done ( including any terminal output).  This helps emphasise that gtkmm code is by definition for a GUI program.  If you connect a member function to a sigc::timeout then you should see the behaviour you want.

Ian

On 31/07/13 14:02, L. D. James wrote:

Thanks, Ian.  This is a great start.  I'll spend some time trying to figure out how to output to a text window or label rather than the graphics bar.

I knew this was possible.  I'm very surprise it's so hard to find a natural way to do it with actual text.

After I get it done, I'll post the resolution back to the group for anyone else that is stuck.

-- L. James

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

On Wed, 2013-07-31 at 12:48 +1200, Ian Martin wrote:
Hi,
Have you looked at the code for the progress bar example?
https://developer.gnome.org/gtkmm-tutorial/stable/sec-progressbar.html.en

Ian.

On 31/07/13 09:33, L. D. James wrote:
> On 07/30/2013 04:53 PM, Moh B. wrote:
>> //main.cpp:
>> //-----------
>> // Look at this modified code and pay attention to the order of
>> execution and call th the functions
>> #include <gtkmm.h>
>> #include <iostream>
>>
>> using namespace std;
>>
>> int main(int argc, char* argv[])
>> {
>>          Gtk::Main kit(argc, argv);
>>
>>          Gtk::Window window;
>>          Gtk::TextView textview;
>>          Gtk::Label label;
>>
>>    string mylabeltext = "This is the first line of text in my gui
>> window.\n";
>>
>>          window.set_default_size(600, 360);
>>          window.set_title("Gtkmm Programming - C++");
>>          window.set_position(Gtk::WIN_POS_CENTER);
>>
>>          label.show();
>>          window.add(label);
>>
>>          label.set_text(mylabeltext);
>>
>>          mylabeltext += "About to run some routines...\n";
>>
>>          label.set_text(mylabeltext);
>>
>>          cout << "An initial line has been set to the gui window." <<
>> endl;
>>          // The Gui Window is displayed
>>    //==> YOURS      Gtk::Main::run(window);
>>    // Now my main program has performed some functions and wants to
>> update
>>          // the console and the gui window.
>>      cout << "Continuing after various functions and processing..." <<
>> endl;
>>           //A MODIFICATION HERE '+=' instead of yours '=''
>>          mylabeltext += "Showing the results of the functions and
>> processing.";
>>          label.set_text(mylabeltext);
>>   // AND NOW
>>     Gtk::Main::run(window);
>>          return 0;
>> }
>> //----------------------------------------------
>> //code end
>>
>> /////////////// Makefile ///////////////////
>> #Begin of Makefile
>> all:
>>
>>     @echo " "
>>     @echo " 1- Compiling..."
>>     g++ -Wall -g -c *.cpp `pkg-config gtkmm-3.0 --cflags`
>>
>>     @echo " "
>>     @echo " 2- Linking..."
>>     g++ -Wall -g *.o `pkg-config gtkmm-3.0 --libs` -o
>> Gtkmm34-popup-menu-test
>>
>>     @echo " "
>>     @echo " DONE!!!";
>> #    @echo " "
>>     chmod +x Gtkmm34-popup-menu-test
>>     ls . -all
>>
>> clean:
>>     rm *.o Gtkmm34-popup-menu-test
>>
>> /////////////// Makefile ///////////////////
>> #End of Makefile
>>
>
> How are you doing, Moh B.
>
> I believe you misunderstood my problem.  I can output all the text out 
> to the gui window without problems.  However, what I'm trying to do is 
> output the text after an operation has taken place.  I should have put 
> sleep() functions to represent some operation taking place.  I want to 
> give the user a status of what is taking place between operations.
>
> I updated the code to better represent this.
>
> At present I see the output at each stage on the console. However, the 
> gui window isn't updated.  It doesn't show anything until after all 
> the functions have completed (in this case sleep() functions.
>
> In the actually program some of the functions will take a few minutes 
> and some of them might take over an hour.  I don't want the user to be 
> left with a no status update for the entire time, or between operations.
>
> Notice how the console is output immediately.  Then a sleep function.  
> Then the status is updated again informing the user how long the 
> function took.  Then another function (in this case a sleep() function.
>
> //code begin:
> //main.cpp:
> //-----------
> // Look at this modified code and pay attention to the order of 
> execution and call th the functions
> #include <gtkmm.h>
> #include <iostream>
>
> using namespace std;
>
> int main(int argc, char* argv[])
> {
>     Gtk::Main kit(argc, argv);
>
>     Gtk::Window window;
>     Gtk::TextView textview;
>     Gtk::Label label;
>
>     string mylabeltext = "This is the first line of text in my 
> guiwindow.\n";
>
>     window.set_default_size(600, 360);
>     window.set_title("Gtkmm Programming - C++");
>     window.set_position(Gtk::WIN_POS_CENTER);
>
>     label.show();
>     window.add(label);
>
>     label.set_text(mylabeltext);
>
>     mylabeltext += "About to run some routines...\n";
>
>     label.set_text(mylabeltext);
>
>     cout << "An initial line has been set to the gui window." << endl;
>
>     sleep(60); // This is the estimated time for a function to run. 
> Some of them takes an hour.
>     mylabeltext += "Continuing after the first fundtion that took 60 
> seconds\n";
>     cout << "Continuing after various functions and processing..." << 
> endl;
>     label.set_text(mylabeltext);
>
>     sleep(60);  // This represents another stage of processes and 
> operations.  I'm attempting to update
>     // the status to the user.
>     //A MODIFICATION HERE '+=' instead of yours '=''
>     mylabeltext += "Other events are taking place that has taken 
> another 60 seconds\n";
>     cout << "Other events have taken place" << endl;
>     label.set_text(mylabeltext);
>     // AND NOW
>     Gtk::Main::run(window);
>     return 0;
> }
> //----------------------------------------------
> //code end
>
> -- L. James
>

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list



_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list



_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list

_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list



_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
https://mail.gnome.org/mailman/listinfo/gtkmm-list



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