Re: What is the minimum number of lines to update a gui window without user clicking a button



I thank everyone who has contributed to this topic and assisted with your valuable suggestions and comments. As a result I'm able to present the solution to my problem. Kjell's input was of paramount value.

I used the first helloworld example from the gtkmm tutorial and added just a few lines and was able to easily update the graphic window with new text. This example took 68 lines.

I replaced the button widget with the textview widget. Added the thread signal as suggested by Kjell. Then added my personal c++ function (cpp_app).

I included a second example with uses a system call function which displays the content of the root directory. This is exactly what I was trying to do. Output/append the results of my functions to the gui screen (or the console or both).

I have two more questions, of which I'll take some time to study and try to figure out a clear and definitive way of asking.

The code is below. I didn't delete the orginal HelloWorld lines from the example. I commented out the unneeded lines.

The last code section (of the 4) is an inclusion a system call function which displays the content of the root directory. This is exactly what I was trying to do. Output/append the results of my functions to the gui screen (or the console or both).

// code begin example (main.cc)
// ----------------------------------------------

// ------------------------
#include "HelloWorld.h"
#include <gtkmm/main.h>

int main (int argc, char *argv[])
{
  Gtk::Main kit(argc, argv);
    if (!Glib::thread_supported())
        Glib::thread_init();

  HelloWorld helloworld;
  //Shows the window and returns when it is closed.
  Gtk::Main::run(helloworld);

  return 0;
}
----------------------
// code end


// code begin example (HelloWorld.h)
// ----------------------------------------------
#ifndef GTKMM_EXAMPLE_HELLOWORLD_H
#define GTKMM_EXAMPLE_HELLOWORLD_H

// #include <gtkmm/button.h>
// #include <gtkmm/window.h>
#include <gtkmm.h>

class HelloWorld : public Gtk::Window
{

public:
  HelloWorld();
  virtual ~HelloWorld();

protected:
  //Signal handlers:
  // // void on_button_clicked();

  //Member widgets:
  Gtk::TextView m_textview;
  void cpp_app();
};

#endif // GTKMM_EXAMPLE_HELLOWORLD_H
// ----------------------------------------------
// code end

// code begin example (HelloWorld.cpp)
// ----------------------------------------------
#include "HelloWorld.h"
#include <iostream>
#include <string>

using namespace std;

HelloWorld::HelloWorld() :
        m_textview()   // creates a new button with label "Hello World".
{
    // Sets the border width of the window.
    // // set_border_width(10);

    // When the button receives the "clicked" signal, it will call the
    // on_button_clicked() method defined below.
    // // m_button.signal_clicked().connect(sigc::mem_fun(*this,
    // //            &HelloWorld::on_button_clicked));

    // This packs the button into the Window (a container).
    add(m_textview);

    // The final step is to display this newly created widget...
    m_textview.show();
    Glib::Thread::create(sigc::mem_fun(*this, &HelloWorld::cpp_app), true);
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::cpp_app()
{
    string texttoprint = "";
    Glib::RefPtr<Gtk::TextBuffer> m_refTextBuffer;
    m_refTextBuffer = Gtk::TextBuffer::create();

    texttoprint +=
"About to run a number of c++ functions\nand display the results\n";
    m_refTextBuffer->set_text(texttoprint);
    m_textview.set_buffer(m_refTextBuffer);

    sleep(10);  // This sleep function that takes 10 seconds to run.

    texttoprint += "Back from running a short 10 second function";
    m_refTextBuffer->set_text(texttoprint);
    m_textview.set_buffer(m_refTextBuffer);
}

// // void HelloWorld::on_button_clicked()
// // {
// //   std::cout << "Hello World" << std::endl;
//  // }

// ----------------------------------------------
// code end


// code begin example (HelloWorld.cpp / with system call form c++ program)
// ----------------------------------------------
/*
 ============================================================================
 Name        : gtkmmHelloworld.cpp
 Author      : Apollo III Communications
 Version     :
 Copyright   : Copyright 2013
 Description : Hello World in gtkmm
 ============================================================================
 */

#include "HelloWorld.h"
#include <iostream>
#include <string>

using namespace std;

HelloWorld::HelloWorld() :
        m_textview()   // creates a new button with label "Hello World".
{
    // Sets the border width of the window.
    // // set_border_width(10);

    // When the button receives the "clicked" signal, it will call the
    // on_button_clicked() method defined below.
    // // m_button.signal_clicked().connect(sigc::mem_fun(*this,
    // //            &HelloWorld::on_button_clicked));

    // This packs the button into the Window (a container).
    add(m_textview);

    // The final step is to display this newly created widget...
    m_textview.show();
    Glib::Thread::create(sigc::mem_fun(*this, &HelloWorld::cpp_app), true);
}

HelloWorld::~HelloWorld()
{
}

void HelloWorld::cpp_app()
{
    string texttoprint = "";
    Glib::RefPtr<Gtk::TextBuffer> m_refTextBuffer;
    m_refTextBuffer = Gtk::TextBuffer::create();
    string runit(std::string c);

    texttoprint +=
"About to run a number of c++ functions\nand display the results\n";
    m_refTextBuffer->set_text(texttoprint);
    m_textview.set_buffer(m_refTextBuffer);

    sleep(10);  // This sleep function that takes 10 seconds to run.

    texttoprint += "Back from running a short 10 second function";
    m_refTextBuffer->set_text(texttoprint);
    m_textview.set_buffer(m_refTextBuffer);

    string ret = runit("ls / | head -5");

    texttoprint += ret;
    m_refTextBuffer->set_text(texttoprint);
    m_textview.set_buffer(m_refTextBuffer);
}

string runit(std::string c)
{
    string lret = "";

    c += " 2>&1";

    FILE *in;
    char buff[512];

    if (!(in = popen(c.c_str(), "r")))
    {
        return lret;
    }

    while (fgets(buff, sizeof(buff), in) != NULL)
    {
        lret += buff;
    }
    pclose(in);

    return lret;
}

// // void HelloWorld::on_button_clicked()
// // {
// //   std::cout << "Hello World" << std::endl;
//  // }
// ----------------------------------------------
// code end

-- L. James

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


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