Results 1 to 4 of 4

Thread: QTextStream with stdout

  1. #1
    Join Date
    Oct 2011
    Location
    Turkey
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default QTextStream with stdout

    Hello. I am trying to do simple hello world console app with Qt. I use Qt 4.7.4 libraries and QtCreator 2.3.1 on Windows 7 x64 Home Premium SP1. When I build and run it nothing happens. Just a blank console appears. Here is code:
    Qt Code:
    1. #include <QtCore/QCoreApplication>
    2. #include <QString>
    3. #include <QTextStream>
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QCoreApplication a(argc, argv);
    8. QString str = "Hello World";
    9. QTextStream out(stdout, QIODevice::WriteOnly);
    10.  
    11. out << str;
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 
    and .pro file:
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2011-12-03T21:44:19
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core
    8.  
    9. QT -= gui
    10.  
    11. TARGET = untitled
    12. CONFIG += console
    13. CONFIG -= app_bundle
    14.  
    15. TEMPLATE = app
    16.  
    17.  
    18. SOURCES += main.cpp
    To copy to clipboard, switch view to plain text mode 
    I tried on OSX Lion 10.7.2 but the result is same. So is it a bug or am I doing something wrong?

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextStream with stdout

    That happens because you start an event-loop that just loops without doing nothing - i'm talking about the creation of that QCoreApplication and the call of exec().

    For your "hello world" app you can skip the QCoreApplication and it's exec, so your main will look something like:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QString str = "Hello World\n";
    4. QTextStream out(stdout, QIODevice::WriteOnly);
    5.  
    6. out << str;
    7. //out.flush(); //a stream flush might be necessary to see the results immediately
    8. return 0;
    9. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Oct 2011
    Location
    Turkey
    Posts
    5
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QTextStream with stdout

    Thanks. It works. But Then why do we need QCoreApplication in console app if it can break something?

  4. #4
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: QTextStream with stdout

    You might need QCoreApplication if, for example, you code a "server"-side application that doesn't have a GUI but it waits for events (eg: some network requests from the "client"-side applications) - this is just the first example that crossed my mind, i'm sure there are other usages.

Similar Threads

  1. How to get the STDOUT from a QTestLib DLL?
    By entonjackson in forum Qt Programming
    Replies: 6
    Last Post: 19th October 2011, 09:02
  2. error in QTextStream cout(stdout)
    By kaushik_acharya in forum Newbie
    Replies: 7
    Last Post: 9th April 2009, 13:24
  3. QTextStream capture stdout from xsltParseStylesheetFile
    By patrik08 in forum Qt Programming
    Replies: 9
    Last Post: 25th June 2006, 11:24
  4. Cannot get stdout from QProcess
    By johnny_sparx in forum Qt Programming
    Replies: 11
    Last Post: 3rd March 2006, 11:46
  5. fstream -> stdout
    By zlatko in forum General Programming
    Replies: 11
    Last Post: 25th January 2006, 05:15

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.