Application output does not wait for QTextStream readLine();
-
wrote on 9 Jun 2022, 13:48 last edited by
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 |
-
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 |
wrote on 9 Jun 2022, 14:12 last edited by@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? -
wrote on 9 Jun 2022, 14:17 last edited by
-
Sadly this did not fix it:

it still does not wait for readLine and prints cout already ):
wrote on 9 Jun 2022, 14:21 last edited by JonB 6 Sept 2022, 14:25@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/4