Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Redirecting all cout statements to textEdit box



  • I'm very new to C++. I've been trying to correctly re-direct all of my cout statements in my program to instead print in a textEdit box in my mainWindow.cpp. Essentially, it would send everything that would have been displayed in the console to this box instead. My issue is that only cout statements that I write in mainwindow.cpp are being redirected to the text box. No cout statements from my main.cpp function is being sent there. The problem is probably stupid but I can't find it, and don't have a huge knowledge of C++. I do, however, need it to be done in this way.

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QWidget>
    #include "main.h"
    using namespace std;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        redirect = new QDebugStream(std::cout, ui->textEdit);
        cout << "test" << endl;
    }
    
    MainWindow::~MainWindow()
    {
        delete redirect;
        delete ui;
    }
    
    
    void MainWindow::on_pushButton_clicked()
    {
    
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QWidget>
    #include <QApplication>
    #include <iostream>
    #include <streambuf>
    #include <string>
    #include "qtextedit.h"
    
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
        class MainWindow;
    }
    QT_END_NAMESPACE
    
    
    class QDebugStream : public std::basic_streambuf<char>
    {
    public:
        QDebugStream(std::ostream &stream, QTextEdit *text_edit) : m_stream(stream)
        {
            log_window = text_edit;
            m_old_buf = stream.rdbuf();
            stream.rdbuf(this);
        }
        ~QDebugStream()
        {
    // output anything that is left
            if (!m_string.empty())
                log_window->append(m_string.c_str());
    
            m_stream.rdbuf(m_old_buf);
        }
    
    protected:
        virtual int_type overflow(int_type v)
        {
            if (v == '\n') {
                log_window->append(m_string.c_str());
                m_string.erase(m_string.begin(), m_string.end());
            } else
                m_string += v;
    
            return v;
        }
    
        virtual std::streamsize xsputn(const char *p, std::streamsize n)
        {
            m_string.append(p, p + n);
    
            int pos = 0;
            while (pos != std::string::npos) {
                pos = m_string.find('\n');
                if (pos != std::string::npos) {
                    std::string tmp(m_string.begin(), m_string.begin() + pos);
                    log_window->append(tmp.c_str());
                    m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
                }
            }
    
            return n;
        }
    
    private:
        std::ostream &m_stream;
        std::streambuf *m_old_buf;
        std::string m_string;
    
    
        QTextEdit *log_window;
    };
    
    
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
        QDebugStream *redirect;
    };
    
    #endif // MAINWINDOW_H
    

    I pulled the rdbuf code in mainwindow.h from online, as it seemed to meet my needs. I tried putting the stream buffer redirection code into the main.h file instead, and it didn't work. What am I missing?



  • @sf001 said in Redirecting all cout statements to textEdit box:

    I'm very new to C++. I've been trying to correctly re-direct all of my cout statements in my program to instead print in a textEdit box in my mainWindow.cpp. Essentially, it would send everything that would have been displayed in the console to this box instead. My issue is that only cout statements that I write in mainwindow.cpp are being redirected to the text box. No cout statements from my main.cpp function is being sent there.

    What does main.cpp look like?

    mainwindow.h

    
    protected:
        virtual int_type overflow(int_type v)
        {
            if (v == '\n') {
                log_window->append(m_string.c_str());
    
    virtual std::streamsize xsputn(const char *p, std::streamsize n)
    {
    

    [...]

                log_window->append(tmp.c_str());
    

    This redirection isn't safe outside of the GUI thread.



  • @sf001 said in Redirecting all cout statements to textEdit box:

    No cout statements from my main.cpp function is being sent there.

    redirect = new QDebugStream(std::cout, ui->textEdit);
    

    redirect object is created in mainwindow. which is using ui->textEdit from mainwindow.
    What have you done in main?
    Do you have any cout messages in main after the mainwindow object creation?



  • https://stackoverflow.com/questions/18086193/redirect-stdout-stderr-to-file-under-unix-c-again

    top link in google search.

    moving the text from the redirected stream is left as an exercise for the reader. Hint involves a stream reader and a signal linked to the TextEdit::SetText() slot.


Log in to reply