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();

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 9 Jun 2022, 13:48 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 |

    J 1 Reply Last reply 9 Jun 2022, 14:12
    0
    • H HoliGui
      9 Jun 2022, 13:48

      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 |

      J Offline
      J Offline
      JonB
      wrote on 9 Jun 2022, 14:12 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 9 Jun 2022, 14:17 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 ):

        J 1 Reply Last reply 9 Jun 2022, 14:21
        0
        • H HoliGui
          9 Jun 2022, 14:17

          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 ):

          J Offline
          J Offline
          JonB
          wrote on 9 Jun 2022, 14:21 last edited by JonB 6 Sept 2022, 14:25
          #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
          • J JonB referenced this topic on 9 Feb 2025, 18:04

          1/4

          9 Jun 2022, 13:48

          • Login

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