Results 1 to 8 of 8

Thread: Routing stdout to Plain Text Edit

  1. #1
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Routing stdout to Plain Text Edit

    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.
    Last edited by Barry79; 2nd April 2009 at 12:00.

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Routing cout to Plain Text Edit

    try this
    Qt Code:
    1. ...
    2. textEdit->textCursor().insertText("hello</br>");
    3. ...
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Routing cout to Plain Text Edit

    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.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Routing stdout to Plain Text Edit

    do you use QProcess to reading stoud or something else?
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Mar 2009
    Posts
    25
    Thanked 2 Times in 2 Posts

    Default Re: Routing stdout to Plain Text Edit

    Adapt the code below. Either install a handler for qDebug() or use QTextEdit instead of qDebug in this class.

    Qt Code:
    1. // Courtesy of "stele"
    2.  
    3. #ifndef Q_DEBUG_H
    4. #define Q_DEBUG_H
    5.  
    6. #include <iostream>
    7. #include <streambuf>
    8. #include <string>
    9.  
    10. class QDebugStream : public std::basic_streambuf<char>
    11. {
    12. public:
    13. QDebugStream(std::ostream &stream) : m_stream(stream)
    14. {
    15. m_old_buf = stream.rdbuf();
    16. stream.rdbuf(this);
    17. }
    18. ~QDebugStream()
    19. {
    20. // output anything that is left
    21. if (!m_string.empty())
    22. qDebug(m_string.c_str());
    23.  
    24. m_stream.rdbuf(m_old_buf);
    25. }
    26.  
    27. protected:
    28. virtual int_type overflow(int_type v)
    29. {
    30. if (v == '\n')
    31. {
    32. qDebug(m_string.c_str());
    33. m_string.clear();
    34. }
    35. else
    36. m_string.push_back(v);
    37.  
    38. return v;
    39. }
    40.  
    41. virtual std::streamsize xsputn(const char *p, std::streamsize n)
    42. {
    43. m_string.append(p, p + n);
    44.  
    45. int pos = 0;
    46. while (pos != std::string::npos)
    47. {
    48. pos = m_string.find('\n');
    49. if (pos != std::string::npos)
    50. {
    51. std::string tmp(m_string.begin(), m_string.begin() + pos);
    52. qDebug(tmp.c_str());
    53. m_string.erase(m_string.begin(), m_string.begin() + pos + 1);
    54. }
    55. }
    56.  
    57. return n;
    58. }
    59.  
    60. private:
    61. std::ostream &m_stream;
    62. std::streambuf *m_old_buf;
    63. std::string m_string;
    64. };
    65.  
    66. #endif
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Routing stdout to Plain Text Edit

    do you use QProcess...

    No, here's an example.

    Qt Code:
    1. sceneGraph = X3dDocParser.ExtractSceneGraphFromFile(x3dFilePath.ToStdString());
    To copy to clipboard, switch view to plain text mode 

    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.

  7. #7
    Join Date
    Mar 2009
    Posts
    25
    Thanked 2 Times in 2 Posts

    Default Re: Routing stdout to Plain Text Edit

    How can I read from stdout so that I can display this info.
    See the previous posts please...

  8. #8
    Join Date
    Feb 2009
    Posts
    21
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Thumbs up Re: Routing stdout to Plain Text Edit

    Perfect! Working now, thanks for your help.

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Replies: 1
    Last Post: 15th January 2009, 10:34
  3. how to read from text edit in QT4
    By iamjayanth in forum Qt Programming
    Replies: 3
    Last Post: 17th October 2008, 12:51
  4. Replies: 8
    Last Post: 15th May 2007, 09:21
  5. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03

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.