There is another problem though. QTextEdit seems to display the content from ostream only when ostream is done taking input. I tried ostream::flush(), it doesn't help. Following is the code segment that uses the QTextEdit and ostream redirection. Any idea why QTextEdit is slow to display?
//------ .cpp file
DeOutputWindow
::DeOutputWindow () : QDockWidget("Output"){
_outputTextEditor->setReadOnly(false);
_outputTextEditor->setTextInteractionFlags(Qt::NoTextInteraction);
_outputStream = new DeOutputStream(std::cout, _outputTextEditor);
setWidget(_outputTextEditor);
}
DeOutputWindow::DeOutputWindow () : QDockWidget("Output")
{
_outputTextEditor = new QTextEdit();
_outputTextEditor->setReadOnly(false);
_outputTextEditor->setTextInteractionFlags(Qt::NoTextInteraction);
_outputStream = new DeOutputStream(std::cout, _outputTextEditor);
setWidget(_outputTextEditor);
}
To copy to clipboard, switch view to plain text mode
//------- .hpp file
{
Q_OBJECT
public:
/// Constructor
DeOutputWindow ();
/// Destructor
virtual ~DeOutputWindow ();
private:
DeOutputStream* _outputStream;
};
class DeOutputWindow : public QDockWidget
{
Q_OBJECT
public:
/// Constructor
DeOutputWindow ();
/// Destructor
virtual ~DeOutputWindow ();
private:
QTextEdit* _outputTextEditor;
DeOutputStream* _outputStream;
};
To copy to clipboard, switch view to plain text mode
/// A class that redirect output to an std::ostream to a QTextEdit
class DeOutputStream : public std::basic_streambuf<char>
{
public:
/** Constructor
* @param out
* The ostream whose content will be redirected to text edit
* @param textEditor
* The window that will display the text written to ostream
*/
DeOutputStream
(std
::ostream& out,
QTextEdit* textEditor
);
/// Destructor
~DeOutputStream ();
protected:
/// Override method that construct the text string and overflow
virtual int_type overflow (std::basic_streambuf<char>::int_type v);
/// Override method that take in the text character
virtual std::streamsize xsputn (const char* p, std::streamsize n);
private:
std::ostream& _out; // ostream whose content will be redirected to text edit
std::streambuf* _oldBuffer; // Old buffer of the ostream
QTextEdit* _textEditor;
// Displaying text editor std::string _text; // Text to be displayed
};
class DeOutputStream : public std::basic_streambuf<char>
{
public:
/** Constructor
* @param out
* The ostream whose content will be redirected to text edit
* @param textEditor
* The window that will display the text written to ostream
*/
DeOutputStream (std::ostream& out, QTextEdit* textEditor);
/// Destructor
~DeOutputStream ();
protected:
/// Override method that construct the text string and overflow
virtual int_type overflow (std::basic_streambuf<char>::int_type v);
/// Override method that take in the text character
virtual std::streamsize xsputn (const char* p, std::streamsize n);
private:
std::ostream& _out; // ostream whose content will be redirected to text edit
std::streambuf* _oldBuffer; // Old buffer of the ostream
QTextEdit* _textEditor; // Displaying text editor
std::string _text; // Text to be displayed
};
To copy to clipboard, switch view to plain text mode
Bookmarks