Results 1 to 3 of 3

Thread: Display content wirtten to STL::ostream dynamically

  1. #1
    Join Date
    Dec 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Display content wirtten to STL::ostream dynamically

    Hi,

    My application writes to an C++ STL output stream (ostream) during the execution of the program. I would like to display the whatever written to the ostream immediately to an widget (such as QDockWidget).

    I have been looking through document and examples related to QTextStream/QTextEdit/QDataStream, and still can't figure out how to do this. For example, for QTextStream, it only takes FILE as an argument but not an ostream. For QTextEdit, it doesn't seem it can do this either.

    Any guide or even an example would be greatly appreciated.

    Regards,
    Kai

  2. #2
    Join Date
    Dec 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display content wirtten to STL::ostream dynamically

    I found the following link in this forum posted a few years ago. The solution described in that link works! Thanks for those whose posted that link.

    http://www.qtforum.org/article/678/R...to-qDebug.html

  3. #3
    Join Date
    Dec 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Display content wirtten to STL::ostream dynamically

    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
    Qt Code:
    1. DeOutputWindow::DeOutputWindow () : QDockWidget("Output")
    2. {
    3.  
    4. _outputTextEditor = new QTextEdit();
    5. _outputTextEditor->setReadOnly(false);
    6. _outputTextEditor->setTextInteractionFlags(Qt::NoTextInteraction);
    7. _outputStream = new DeOutputStream(std::cout, _outputTextEditor);
    8. setWidget(_outputTextEditor);
    9. }
    To copy to clipboard, switch view to plain text mode 
    //------- .hpp file
    Qt Code:
    1. class DeOutputWindow : public QDockWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. /// Constructor
    6. DeOutputWindow ();
    7. /// Destructor
    8. virtual ~DeOutputWindow ();
    9.  
    10. private:
    11. QTextEdit* _outputTextEditor;
    12. DeOutputStream* _outputStream;
    13. };
    To copy to clipboard, switch view to plain text mode 

    /// A class that redirect output to an std::ostream to a QTextEdit
    Qt Code:
    1. class DeOutputStream : public std::basic_streambuf<char>
    2. {
    3. public:
    4. /** Constructor
    5.   * @param out
    6.   * The ostream whose content will be redirected to text edit
    7.   * @param textEditor
    8.   * The window that will display the text written to ostream
    9.   */
    10. DeOutputStream (std::ostream& out, QTextEdit* textEditor);
    11. /// Destructor
    12. ~DeOutputStream ();
    13.  
    14. protected:
    15. /// Override method that construct the text string and overflow
    16. virtual int_type overflow (std::basic_streambuf<char>::int_type v);
    17.  
    18. /// Override method that take in the text character
    19. virtual std::streamsize xsputn (const char* p, std::streamsize n);
    20.  
    21. private:
    22. std::ostream& _out; // ostream whose content will be redirected to text edit
    23. std::streambuf* _oldBuffer; // Old buffer of the ostream
    24. QTextEdit* _textEditor; // Displaying text editor
    25. std::string _text; // Text to be displayed
    26. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 8th January 2009 at 09:49.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.