Redirect std :: cout to QTextEdit

Is it possible (and more importantly -how-) to redirect the output stream to a QTextBox. So if I write std::cout << "test" anywhere in the application, is it redirected to the text box that I defined?

I tried the obvious (where ui.textEdit is a pointer to a text edit field):

 std::cout.rdbuf(ui.textEdit); std::cout << "test"; 

However, this does not work. (Obviously). - Does not redirect cout to qDebug (or even direct qDebug to a text field).

I am using qt4.8 btw ...

EDIT: So I tried the solution posted on the mailing list. However, an access violation now appears.

 class MainInterface : public QMainWindow { Q_OBJECT .... private: QDebugStream qout 

With constructor:

 MainInterface::MainInterface(QWidget *parent, Qt::WFlags flags) : QMainWindow(parent, flags), qout(std::cout, ui.textEdit) { 

And the following line is laid out in the member function: std::cout << "Project Loaded" << std::endl;

This line now causes an access violation with qscoped_pointer. (Should I post this in more detail on a specific issue?)

EDIT: well, the β€œsolution” was to declare qout only after the ui.textEdit was fully created.

+4
source share
2 answers

You could reset cout to execute your own ostream implementation, which will signal the emit that you are connecting to the append slot. This way you do sub-problems / exercises:

  • redirect cout
  • redirect cout to your own ostream implementation or version that you can extend
  • emit signals

These subtopics are available on SO as far as I know.

+1
source

I wrote my own function for this problem, for QTextEdit, just keep in mind that if you run it with heavy operations along the main thread, your GUI will freeze. Thus, you need to create an instance of a new QThread, for example, a GUI, accordingly, QTextEdit will be updated accordingly:

Header file:

 class myConsoleStream : public std::basic_streambuf<char> { public: myConsoleStream(std::ostream &stream, QTextEdit* text_edit); virtual ~myConsoleStream(); static void registerMyConsoleMessageHandler(); private: static void myConsoleMessageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg); protected: // Diese Funktion wird aufgerufen wenn std::endl im Stream erscheint virtual int_type overflow(int_type v) { if (v == '\n') { log_window->append(""); } return v; } virtual std::streamsize xsputn(const char *p, std::streamsize n); private: std::ostream &m_stream; std::streambuf *m_old_buf; QTextEdit* log_window; }; #endif // Q_DEBUGSTREAM_H 

.cpp file:

 myConsoleStream::myConsoleStream(std::ostream &stream, QTextEdit* text_edit) :std::basic_streambuf<char>() ,m_stream(stream) { this->log_window = text_edit; this->m_old_buf = stream.rdbuf(); stream.rdbuf(this); } myConsoleStream::~myConsoleStream() { this->m_stream.rdbuf(this->m_old_buf); } void myConsoleStream::registerMyConsoleMessageHandler() { qInstallMessageHandler(myConsoleMessageHandler); } void myConsoleStream::myConsoleMessageHandler(QtMsgType type, const QMessageLogContext &, const QString &msg) { QByteArray localMsg = msg.toLocal8Bit(); switch (type) { case QtDebugMsg: // fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtInfoMsg: // fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtWarningMsg: // fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtCriticalMsg: //fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtFatalMsg: // fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; default: std::cout << msg.toStdString().c_str(); break; } } 

In your main window, you just need to instantiate a new thread:

 new myConsoleStream(std::cout, this->ui->Console); myConsoleStream::registerMyConsoleMessageHandler(); 

and you are good too! Hope this helps.

0
source

Source: https://habr.com/ru/post/1395601/


All Articles