Results 1 to 6 of 6

Thread: Rerouting std::cout to a QPlainTextEdit

  1. #1
    Join Date
    Nov 2010
    Posts
    23
    Thanks
    2
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Rerouting std::cout to a QPlainTextEdit

    Hello,

    I have a bit of an odd question, but I am wondering if it is possible to route the std::cout output into a QPlainTextEdit widget. I'm trying to provide a GUI application with textual feedback on the processing it is doing, but I am using some libraries that output to std::cout and thus the information they return is being lost.

    Any suggestions would be appreciated!

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Rerouting std::cout to a QPlainTextEdit

    Yes, it is possible, you need change the cout objects's stream buffer, and then interface this stream buffer with QPlainTextEdit, that should be fairly simple.

    have look at this example, how to get or set stream buffer

    http://www.cplusplus.com/reference/iostream/ios/rdbuf/

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rerouting std::cout to a QPlainTextEdit

    Hi.
    I'm working on a plugin for a 3rd party Qt application.
    I can access the qApp and I also have a pointer to the QMainWindow of this application.
    I successfully managed to redirect stdout to a file by using this code:
    Qt Code:
    1. freopen("C:\output.txt", "w", stdout);
    To copy to clipboard, switch view to plain text mode 

    But how can I redirect stdout to a QTextEdit or QPlainTextEdit widget?
    I tried to do it using the QDebugStream method but it only printed to the QPlainTextEdit when I was explicitly calling cout<<"test"<<endl;
    The QApplication itself still continued printing into the console window.

    I also thought about reimplementing qInstallMsgHandler but somebody told me this is only for qDebug and not for stdout.
    Maybe this can be done using QTextStream?
    By the way I'm using Windows.
    Thanks for any help!
    Last edited by wannabe; 12th September 2013 at 04:24.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rerouting std::cout to a QPlainTextEdit

    Did you try what Santosh Reddy suggested?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Sep 2013
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Rerouting std::cout to a QPlainTextEdit

    You are right. Santosh Reddy has the answer.
    Thanks Santosh
    Last edited by wannabe; 12th September 2013 at 10:54.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Rerouting std::cout to a QPlainTextEdit

    Then look again at what Santosh posted. Then read it, understand it and implement it.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Qt Creator where is cout in Qt Creator
    By hqking1988 in forum Qt Tools
    Replies: 8
    Last Post: 12th September 2019, 22:32
  2. where is cout
    By hqking1988 in forum General Programming
    Replies: 3
    Last Post: 22nd August 2010, 03:35
  3. Rerouting messages for keyboard shortcut selection dialog.
    By andy.fillebrown in forum Qt Programming
    Replies: 1
    Last Post: 15th June 2009, 18:55
  4. cout problem
    By mickey in forum General Programming
    Replies: 3
    Last Post: 2nd March 2007, 18:12
  5. How to cout in Qt ?
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 15th December 2006, 00:47

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
  •  
Qt is a trademark of The Qt Company.