0

有人可以帮助消除如何在没有用户输入的情况下更新 gui 窗口的困惑。

换句话说,我希望能够将文本输出到控制台和 gui 窗口中的一个或两个。

目前我可以调用gui窗口(例如带有标签的窗口)并输出初始文本。但是,在窗口关闭之前,该过程不会返回到我的 c++ 代码。我试图弄清楚如何(或在哪里有我的代码)在 gui 窗口退出之前更新 gui 屏幕。

这是一个例子:

#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
    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;
    mylabeltext = "Showing the results of the functions and processing.";
    label.set_text(mylabeltext);

    return 0;
}

在退出 gui 之前,最后一行文本永远不会打印到控制台。mylabeltext 的最后一行永远不会打印到标签窗口。

我要描述的是如何在我的 c++ 代码中运行其他例程时保持 gtkmm 窗口处于活动状态,并将输出更新到控制台和 gui 窗口而不关闭 gui 窗口以继续 c++ 例程。

我能找到的所有示例都在代码中使用了一个按钮。我已经进行了足够的测试和实验,可以在按下按钮后更新 gui 屏幕。但是,我不想依赖用户进行屏幕更新。我希望能够运行磁盘扫描和其他功能并定期更新屏幕,以便用户可以看到进度并知道程序仍在运行并且没有死机。

我在尝试理解这一点时研究的一些资源包括:

4

2 回答 2

1

就像 tp1 在他们对您的问题的评论中所说的那样,计时器将是最简单的方法。

要设置将调用另一个函数的 1.5 秒超时,请执行以下操作 (gtkmm 3):

#include <gtkmm.h>
#include <iostream>

using namespace std;

class MyApp : public Gtk::Window{
 public:
    Gtk::Label label;
    bool on_timeout(); //return true to keep the timeout and false to end it
    MyApp();
    virtual ~MyApp();
};

MyApp::MyApp(){
 string mylabeltext = "This is the first line of text in my gui window.\n";

 set_default_size(600, 360);
 set_title("Gtkmm Programming - C++");
 set_position(Gtk::WIN_POS_CENTER);

 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;

 //create slot for timeout signal
 int timeout_value = 1500; //in ms (1.5 sec)
 sigc::slot<bool>my_slot = sigc::mem_fun(*this, &MyApp::on_timeout);
 //connect slot to signal
 Glib::signal_timeout().connect(my_slot, timeout_value);
 show_all_children();
}

MyApp::~MyApp(){

}

bool MyApp::on_timeout(){
 cout << "Continuing after various functions and processing..." << endl;
 string temp = label.get_text();
 temp += "Showing the results of the functions and processing.\n";
 label.set_text(temp);
 return true;
}

int main(int argc, char* argv[])
{
 Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "com.kaze.test");

 MyApp myapp;

 // The Gui Window is displayed
 return app->run(myapp);
}

更多信息:https ://developer.gnome.org/gtkmm-tutorial/3.3/sec-timeouts.html.en

于 2013-07-30T16:39:17.483 回答
0

这是粗略的,但这对于我试图做的事情是有用的:

#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;
}

希望该示例可以帮助任何想要通过更新输出到 gtkmm gui 的人,类似于将信息更新到控制台。

于 2013-08-06T02:14:03.440 回答