Results 1 to 5 of 5

Thread: Tried to redirect cout & cerr to QTextEdit instead of Console, but not working!

  1. #1
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Question Tried to redirect cout & cerr to QTextEdit instead of Console, but not working!

    Hello! Friends,
    I have designed a GUI in which i have lots of cout & cerr messages. Right now am able to see all those in console window but i want them to be displayed in my GUI's QTextEdit widget.

    For this i made forum search and found this from where i got to know that i will have to make my own streaming class that inherits std:stream and outputs everything using qDebug or some completely different mechanism. Also here i found that i can use qdebugstream.h header to do the needful, but its not working May be i might have made mistake in implementing it.

    Here is what i tried: -
    Qt Code:
    1. #include <QtGui>
    2. #include <iostream>
    3. #include "qdebugstream.h"
    4.  
    5. using namespace std;
    6.  
    7. #include "log.h"
    8.  
    9. mylog::mylog(QWidget *parent)
    10. :QWidget( parent )
    11. {
    12. setupUi(this); // this sets up GUI
    13. mylog::setWindowTitle(QString("Checking log"));
    14.  
    15. connect( Start, SIGNAL( clicked() ), this, SLOT( examine() ) );
    16. }
    17.  
    18. void mylog::examine()
    19. {
    20. cout <<"Hello! We are ready for working" << endl;
    21. QDebugStream cout(std::cout,logTextEdit);
    22. QDebugStream cerr(std::cerr,logTextEdit);
    23. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone please let me know where am i making mistake. Or is there any other way to do the needful.

    I will be grateful if helped--> Thanks in advance.

  2. #2
    Join Date
    Feb 2008
    Posts
    26
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Tried to redirect cout & cerr to QTextEdit instead of Console, but not working!

    You're setting the stream after sending the text to cout. You should do it the other way around: first, set the streams, then, start sending text to it.

  3. #3
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Re: Tried to redirect cout & cerr to QTextEdit instead of Console, but not working!

    Hello! Stukfruit,
    Thanks for suggesting me that, but i have already tried that out by which i got following error's: -
    Qt Code:
    1. void mylog::examine()
    2. {
    3. QDebugStream cout(std::cout,logTextEdit);
    4. QDebugStream cerr(std::cerr,logTextEdit);
    5. cout <<"Hello! We are ready for working" << endl;
    6. }
    To copy to clipboard, switch view to plain text mode 

    #########error's#########
    In file included from log.cpp:5:
    qdebugstream.h: In member function `virtual std::streamsize QDebugStream::xsputn
    (const char*, std::streamsize)':
    qdebugstream.h:50: warning: comparison between signed and unsigned integer expre
    ssions
    qdebugstream.h:53: warning: comparison between signed and unsigned integer expre
    ssions
    In file included from log.cpp:5:
    qdebugstream.h:74:7: warning: no newline at end of file
    log.cpp: In member function `void mylog::examine()':
    log.cpp:27: error: no match for 'operator<<' in 'cout << "Hello! We are ready fo
    r working"'
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qchar.h:365: note: cand
    idates are: QDataStream& operator<<(QDataStream&, const QChar&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qbytearray.h:534: note:
    QDataStream& operator<<(QDataStream&, const QByteArray&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qstring.h:1008: note:
    QDataStream& operator<<(QDataStream&, const QString&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qobject.h:457: note:
    QDebug operator<<(QDebug, const QObject*)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/io/qiodevice.h:244: note:
    QDebug operator<<(QDebug, QFlags<QIODevice::OpenModeFlag>)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/io/qtextstream.h:286: note:
    QTextStream& operator<<(QTextStream&, QTextStream&(*)(QTextStream
    &))
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/io/qtextstream.h:289: note:
    QTextStream& operator<<(QTextStream&, QTextStreamManipulator)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qregexp.h:141: note:
    QDataStream& operator<<(QDataStream&, const QRegExp&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qstringlist.h:229: note
    : QDataStream& operator<<(QDataStream&, const QStringList&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qlocale.h:579: note:
    QDataStream& operator<<(QDataStream&, const QLocale&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/io/qurl.h:250: note:
    QDataStream& operator<<(QDataStream&, const QUrl&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/io/qurl.h:255: note:
    QDebug operator<<(QDebug, const QUrl&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qvariant.h:590: note:
    QDebug operator<<(QDebug, const QVariant&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qvariant.h:496: note:
    QDataStream& operator<<(QDataStream&, const QVariant&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qvariant.h:498: note:
    QDataStream& operator<<(QDataStream&, QVariant::Type)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/kernel/qvariant.h:591: note:
    QDebug operator<<(QDebug, QVariant::Type)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qpoint.h:102: note:
    QDataStream& operator<<(QDataStream&, const QPoint&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qpoint.h:182: note:
    QDebug operator<<(QDebug, const QPoint&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qpoint.h:236: note:
    QDataStream& operator<<(QDataStream&, const QPointF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qpoint.h:357: note:
    QDebug operator<<(QDebug, const QPointF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qline.h:161: note:
    QDebug operator<<(QDebug, const QLine&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qline.h:165: note:
    QDataStream& operator<<(QDataStream&, const QLine&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qline.h:321: note:
    QDebug operator<<(QDebug, const QLineF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qline.h:325: note:
    QDataStream& operator<<(QDataStream&, const QLineF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qsize.h:101: note:
    QDataStream& operator<<(QDataStream&, const QSize&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qsize.h:196: note:
    QDebug operator<<(QDebug, const QSize&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qsize.h:252: note:
    QDataStream& operator<<(QDataStream&, const QSizeF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qsize.h:357: note:
    QDebug operator<<(QDebug, const QSizeF&)
    ../../../Qt/4.3.4/include/QtCore/../../src/corelib/tools/qrect.h:198: note:
    QDataStream& operator<<(QDataStream&, const QRect&)
    ........
    ..........
    ..............
    ...............This continues & finally ../..

    ../../../Qt/4.3.4/include/QtGui/../../src/gui/itemviews/qtreewidget.h:248: note:
    QDataStream& operator<<(QDataStream&, const QTreeWidgetItem&)
    ../../../Qt/4.3.4/include/QtGui/../../src/gui/graphicsview/qgraphicsitem.h:910:
    note: QDebug operator<<(QDebug, QGraphicsItem*)
    ../../../Qt/4.3.4/include/QtGui/../../src/gui/widgets/qsplitter.h:152: note:
    QTextStream& operator<<(QTextStream&, const QSplitter&)
    mingw32-make[1]: *** [release/log.o] Error 1
    Please help me in solving the problem.
    Thanks again.
    Last edited by jpn; 27th March 2008 at 10:23. Reason: missing tags

  4. #4
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Tried to redirect cout & cerr to QTextEdit instead of Console, but not working!

    Quote Originally Posted by Krish View Post
    Qt Code:
    1. QDebugStream cout(std::cout, textEdit);
    2. QDebugStream cerr(std::cerr, textEdit);
    3. cout <<"Hello! We are ready for working" << endl;
    To copy to clipboard, switch view to plain text mode 
    You are trying to write to QDebugStream and you should write to std::cout, so try this:
    Qt Code:
    1. std::cout <<"Hello! We are ready for working" << std::endl;
    To copy to clipboard, switch view to plain text mode 
    instead.
    The Wheel weaves as the Wheel wills.

  5. The following user says thank you to danadam for this useful post:

    Krish (27th March 2008)

  6. #5
    Join Date
    Feb 2008
    Posts
    74
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Solved->Triedtoredirect cout & cerr to QTextEdit instead of Console, but not working!

    Hello! danadam,
    Thanks for taking out time & replying me. I had include the std namespace at the beginning of the implementation, but it wasn't working.
    But now i tried to separately add std::cout its working.

    Thanks again & Best Regards.

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.