owen_263
9th July 2008, 05:43
The class is provided like this,
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);
std::string::size_type 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;
};
The Usage is like this:
[...]
QTexEdit* myTextEdit = new QTextEdit(this, "myTextEdit");
myTextEdit->setTextFormat(Qt::LogText);
QDebugStream qout(std::cout, myTextEdit);
std::cout << "Send this to the Text Edit!" << endl;
[...]
After the EXC_BAD_ACCESS error, GDB backtrace shows:
#0 0x013b8819 in QTextEdit::append ()
#1 0x000108e7 in QDebugStream::overflow (this=0xbffff390, v=10) at qDebugStream.h:76
frame 1 is at:
" log_window->append(m_string.c_str()); "
I guess the problem is from the header file. Anyone could help me? I will appreciate that.
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);
std::string::size_type 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;
};
The Usage is like this:
[...]
QTexEdit* myTextEdit = new QTextEdit(this, "myTextEdit");
myTextEdit->setTextFormat(Qt::LogText);
QDebugStream qout(std::cout, myTextEdit);
std::cout << "Send this to the Text Edit!" << endl;
[...]
After the EXC_BAD_ACCESS error, GDB backtrace shows:
#0 0x013b8819 in QTextEdit::append ()
#1 0x000108e7 in QDebugStream::overflow (this=0xbffff390, v=10) at qDebugStream.h:76
frame 1 is at:
" log_window->append(m_string.c_str()); "
I guess the problem is from the header file. Anyone could help me? I will appreciate that.