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. Application output does not wait for QTextStream readLine();
Forum Update on Monday, May 27th 2025

Application output does not wait for QTextStream readLine();

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 793 Views
  • 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.
  • H Offline
    H Offline
    HoliGui
    wrote on last edited by
    #1

    Hi,

    I have a simple Qt console application, but it displays the next string before the input has been read. Any ideas why?

    void do_qt()
    {
        QTextStream qin(stdin);
        QTextStream qout(stdout);
    
        qout << "Please enter your name: ";
    
        qout.flush();
    
        QString name = qin.readLine();
    
        qout << "hello " << name;
        qout.flush();
    }
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        do_qt();
    
        return a.exec();
    }
    

    Thats the output I see as soon as i run the code: Please enter your name: hello |

    JonBJ 1 Reply Last reply
    0
    • H HoliGui

      Hi,

      I have a simple Qt console application, but it displays the next string before the input has been read. Any ideas why?

      void do_qt()
      {
          QTextStream qin(stdin);
          QTextStream qout(stdout);
      
          qout << "Please enter your name: ";
      
          qout.flush();
      
          QString name = qin.readLine();
      
          qout << "hello " << name;
          qout.flush();
      }
      
      int main(int argc, char *argv[])
      {
          QCoreApplication a(argc, argv);
      
          do_qt();
      
          return a.exec();
      }
      

      Thats the output I see as soon as i run the code: Please enter your name: hello |

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by
      #2

      @HoliGui
      Suggestion is https://stackoverflow.com/a/26192090/489865. Not sure why they want you to flush stdin/skip whitespace, but that's what they claim?

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HoliGui
        wrote on last edited by
        #3

        Sadly this did not fix it:

        cd998480-5e8f-4c9a-bf9a-71e4b2c518a6-image.png ![alt text](image url)

        it still does not wait for readLine and prints cout already ):

        JonBJ 1 Reply Last reply
        0
        • H HoliGui

          Sadly this did not fix it:

          cd998480-5e8f-4c9a-bf9a-71e4b2c518a6-image.png ![alt text](image url)

          it still does not wait for readLine and prints cout already ):

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @HoliGui
          Sorry I deleted my previous post with that refence when I realized it was not your case. I didn't know you had read it/started to reply! [Just put it back in now to make sense.]

          I admit I am puzzled. If you replace QString name = qin.readLine(); by:

          QString name;
          qin >> name;
          

          does that make any difference or behave as before without waiting?

          And btw, do you run this either outside Qt Creator in a terminal or within Creator but telling it to open a terminal? It's no good if you just run it in Creator and only look in the Application Output window.

          UPDATE
          Oh, I see from the screenshot that is exactly what you are trying to do! You can't. Application Output window is only for output, your program has no input to read from. Follow one of the two suggestions in previous paragraph.

          1 Reply Last reply
          1
          • JonBJ JonB referenced this topic on

          • Login

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