Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Redirecting all cout statements to textEdit box
QtWS25 Last Chance

Redirecting all cout statements to textEdit box

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 4 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sf001
    wrote on last edited by
    #1

    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?

    jeremy_kJ 1 Reply Last reply
    0
    • S sf001

      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?

      jeremy_kJ Offline
      jeremy_kJ Offline
      jeremy_k
      wrote on last edited by
      #2

      @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.

      Asking a question about code? http://eel.is/iso-c++/testcase/

      1 Reply Last reply
      0
      • nageshN Offline
        nageshN Offline
        nagesh
        wrote on last edited by
        #3

        @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?

        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved