1

The first output in the textedit is a number 3, I don't know why that number is coming form Qt::LogText. This question is based from a previous question I had asked, I'm using the same qdebugstream header file from the link below.

Redirect std::cout to a QTextEdit

The new project below is a QT Gui Application that will redirect the cout to a textedit. Also, since settextformat() is no longer a member of QTextEdit, I converted Qt::LogText into a string.

This was based on another post but I did not understand the solution. QTextEdit::setTextFormat(Qt::LogText) does not exist anymore, what else can I use to log?. Can someone provide more information on this?

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    ui->textEdit->setReadOnly(true);
    ui->textEdit->setText(QString("%1").arg(Qt::LogText));

    QDebugStream qout(std::cout, ui->textEdit);

    cout << "Send this to the Text Edit!" << endl;

}

MainWindow::~MainWindow()
{
    delete ui;
}

Mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "qdebugstream.h"
#include "stdio.h"
#include "iostream"

using namespace std;

namespace Ui {
class MainWindow;
}

class QtextEdit;

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
Community
  • 1
  • 1
Ninja Joey
  • 77
  • 2
  • 6

1 Answers1

2

You should use QPlainTextEdit in Qt 4.x which seems to be equivalent using Q3TextEdit(QTextEdit in Qt 3.x) with text format of Qt::LogText.

For QPlainTextEdit version of QDebugStream might be like followings.(you may be noticed that append() mem func is renamed into addPlainText() and that's it viola). Hope this helps.

#ifndef QDEBUGSTREAM_H
#define QDEBUGSTREAM_H

#include <iostream>
#include <streambuf>
#include <string>
#include <QPlainTextEdit>

class QDebugStream : public std::basic_streambuf<char>
{
public:
    QDebugStream(std::ostream &stream, QPlainTextEdit* 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->appendPlainText(m_string.c_str());

        m_stream.rdbuf(m_old_buf);
    }

protected:
    virtual int_type overflow(int_type v)
    {
        if (v == '\n')
        {
            log_window->appendPlainText(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->appendPlainText(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;


    QPlainTextEdit* log_window;
};

#endif
Joonhwan
  • 495
  • 5
  • 11