【问题标题】:How do I redirect standard output messages to FLTK's Fl_Multiline_Output?如何将标准输出消息重定向到 FLTK 的 Fl_Multiline_Output?
【发布时间】:2019-11-15 16:00:05
【问题描述】:
Fl_Multiline_Output* m_pLogOutput;

m_pLogOutput = new Fl_Multiline_Output(20, 330, 570, 530, "Log Output:");
m_pLogOutput->align(FL_ALIGN_TOP_LEFT);

我想将打印到标准输出(如 std::cout

【问题讨论】:

  • 有几种方法可以做到这一点。 Fl_Multiline_Output 的问题是它没有附加方法。每当输出 endl 时,您都需要累积字符串并设置值。这样做的问题是它总是滚动回第 1 行。这在文本开始滚动之前并不明显。
  • @cup 感谢您的回复!您如何实现“每当输出 endl 时设置值”。部分?

标签: c++ fltk


【解决方案1】:

类似的东西 - 这个例子使用 TextDisplay 而不是 MultiLineOutput。 TextDisplay 有滚动条:MultilineOutput 没有。这是用FLTK2编写的,调用类似于FLTK1,没有FL_和单词之间的下划线。

它定义了一个名为 ConOut 的类。请注意,您不得

#include <iostream>

因为 cout 被重新定义了。

#include <fltk/run.h>
#include <fltk/ValueInput.h> // necessary for bug in mingw32?
#include <fltk/Window.h>
#include <fltk/Button.h>
#include <fltk/TextDisplay.h>
#include <sstream>

using namespace fltk;

class ConOut: public std::ostringstream
{
public:
    ConOut& operator << (std::ostream&(*f)(std::ostream&))
    {
        if (f == std::endl)
        {
            *this << "\n";
            std::string cumulative = control->text() + str();
            control->text(cumulative.c_str());
            str("");
        }
        else
        {
            // Don't worry about the warning
            *this << f;
        }
        return *this;
    }

    void SetWidget(TextDisplay* in_control)
    {
        control = in_control;
    }

    TextDisplay* control;

    template <typename T>
    inline ConOut& operator << (const T& t)
    {
        (*(std::ostringstream*) this) << t;
        return *this;
    }
};

ConOut cout;

void cb_cauli(Widget*, void*)
{
    cout << "Cauliflower" << std::endl;
}

void cb_brocolli(Widget*, void*)
{
    cout << "Brocolli" << std::endl;
}

void cb_cabbage(Widget*, void*)
{
    cout << "Cabbage" << std::endl;
}

int main(int argc, char **argv)
{
    int btnw = 100, btnh = 30, bdr = 10;
    int dlgw = bdr * 4 + 3 * btnw, dlgh = 200 + btnh + 3 * bdr, x, y, w, h;
    Window * window = new Window(dlgw, dlgh);
    window->begin();

    // Create the multiline output
    x = bdr;
    y = bdr;
    w = dlgw - 2 * bdr;
    h = 200;
    TextDisplay* text2 = new TextDisplay(x, y, w, h,"");
    text2->clear_flag(fltk::ALIGN_MASK);
    text2->set_flag(fltk::ALIGN_BOTTOM);
    window->resizable(text2);

    // Create the buttons which use cout
    y += h + bdr;
    w = btnw; h = btnh;
    Button* cauli = new Button(x, y, w, h, "Cauliflower");
    cauli->callback(cb_cauli);
    x += w + bdr;
    Button* brocolli = new Button(x, y, w, h, "Brocolli");
    brocolli->callback(cb_brocolli);
    x += w + bdr;
    Button* cabbage = new Button(x, y, w, h, "Cabbage");
    cabbage->callback(cb_cabbage);

    // Set the widget for cout
    cout.SetWidget(text2);

    window->end();
    window->show(argc,argv);
    return fltk::run();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 2012-08-15
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多