PDA

View Full Version : Routing stdout to Plain Text Edit



Barry79
2nd April 2009, 11:17
Hi,

I run certain functions from within my Qt4 application which write to stdout. I'd like to display this information in a window in my app. How might I go about doing this?

Thanks,

Barry.

spirit
2nd April 2009, 11:30
try this


...
textEdit->textCursor().insertText("hello</br>");
...

Barry79
2nd April 2009, 13:00
Thanks for the reply.

I need to route the stdout to my text window. This is because certain functions write status information to the stdout and I would like to make it possible for users to see this in a text edit window. So how do I route from stdout and stderr to my text edit window?

Thanks for your help,

Barry.

spirit
2nd April 2009, 13:02
do you use QProcess to reading stoud or something else?

Hiker Hauk
2nd April 2009, 13:08
Adapt the code below. Either install a handler for qDebug() or use QTextEdit instead of qDebug in this class.



// Courtesy of "stele"

#ifndef Q_DEBUG_H
#define Q_DEBUG_H

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

class QDebugStream : public std::basic_streambuf<char>
{
public:
QDebugStream(std::ostream &stream) : m_stream(stream)
{
m_old_buf = stream.rdbuf();
stream.rdbuf(this);
}
~QDebugStream()
{
// output anything that is left
if (!m_string.empty())
qDebug(m_string.c_str());

m_stream.rdbuf(m_old_buf);
}

protected:
virtual int_type overflow(int_type v)
{
if (v == '\n')
{
qDebug(m_string.c_str());
m_string.clear();
}
else
m_string.push_back(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);
qDebug(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;
};

#endif

Barry79
2nd April 2009, 13:17
do you use QProcess...

No, here's an example.




sceneGraph = X3dDocParser.ExtractSceneGraphFromFile(x3dFilePath .ToStdString());



This function might print the following to stdout-

Reading Document...
Failed to parse line 20.
Continuing anyway...
Failed to parse line 130.
Continuing anyway...
Reached File End.

The thing is that this function (a part of an Open Source API which I use) does not throw exceptions when it finds problems in the file, it simply ignores them. Therefore I need to print this text within my app so that the user can deal with it as they see fit, i.e.fix whatever formatting error they have in their file.

So, X3dDocParser.ExtractSceneGraphFromFile() is wring to stdout via cout calls.

How can I read from stdout so that I can display this info.

Thanks again,

Barry.

Hiker Hauk
2nd April 2009, 13:21
How can I read from stdout so that I can display this info.


See the previous posts please...

Barry79
2nd April 2009, 14:06
Perfect! Working now, thanks for your help.