Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt Console Application Problems
Qt 6.11 is out! See what's new in the release blog

Qt Console Application Problems

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mahmoud899
    wrote on last edited by
    #1

    Hello, I am having difficulties to understand why my program is being executed wrong. When I run the program, the console screen appears and awaits from me to enter a number, and when I enter a number then press enter, it then prints the following:

    Hello World
    Please Enter Your Age
    Your Age is
    56

    Any suggestions as why is this happening?? Thank You,
    @
    #include <QTextStream>

    int main(int argc, char *argv[])
    {
    int age;

    QTextStream output(stdout);
    QTextStream input(stdin);
    
    output << "Hello World\n\n";
    output << "Please Enter Your Age:\n";
    
    input >> age;
    
    output << "Your Age is: \n";
    output << age;
    

    }
    @

    Mahmoud Ramy

    1 Reply Last reply
    0
    • M Offline
      M Offline
      MuldeR
      wrote on last edited by
      #2

      What about:

      @QFile myStdOut;
      file.open(stdout, QIODevice::WriteOnly | QIODevice::Unbuffered);

      QTextSTream output(&myStdOut)
      output << "Hello World\n\n";
      output << "Please Enter Your Age:\n";

      input >> age;@

      --

      Note however:

      The "stdout" and "stderr" streams have their own buffering too! If they are connected directly to a "real" console, this is disabled automatically. But as soon as the output of a console application gets redirect, e.g. your console application is created as a child process via QProcess, you will notice the buffering! That means, regardless of the QTextStream or QFile buffering, you should be doing this, in order to make sure outputs aren't delayed:

      @for(int i = 0; i <= 100; i++)
      {
      //Do some work that takes time
      doSomeWork(i);

      //Write current progress
      fprintf(stdout, "Progress: %d %%\n", i);
      
      //required flushing!
      fflush(stdout);
      

      }@

      My OpenSource software at: http://muldersoft.com/

      Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

      Go visit the coop: http://youtu.be/Jay...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mahmoud899
        wrote on last edited by
        #3

        Thank you for all of your replies, the output.flush() worked well.

        Mahmoud Ramy

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved