0

When I run the basic Qt code in the below snippet, which is supposed to output "Please enter your name: ", wait for me to enter my name, and then print "Hello < name >", both outputs are written simultaneously, even though it is supposed to prompt for input before the 2nd output is printed.

#include <QCoreApplication>

#include <QTextStream>

void do_qt(){
    QTextStream qin(stdin);
    QTextStream qout(stdout);

    qout << "Please enter your namne: ";
    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();
}

Output :

Please enter your namne: Hello

What is going on here?

I am using:

  • Qt 6.5.2
  • Qt Creator
  • CMake
  • Ubuntu 20.
  • You do not check for failure after `qin.readLine()`. Always check after any input. – hyde Aug 11 '23 at 09:02
  • Does this answer your question? [In Qt6 cin/getline does not read any input for me](https://stackoverflow.com/questions/70506485/in-qt6-cin-getline-does-not-read-any-input-for-me) – Abderrahmene Rayene Mihoub Aug 11 '23 at 09:55
  • [Qt reading from console and stop when Enter has pressed](https://stackoverflow.com/questions/45460731/qt-reading-from-console-and-stop-when-enter-has-pressed) | [Using cin in QtCreator](https://stackoverflow.com/questions/1484950/using-cin-in-qtcreator) | [How to get input from console in QT?](https://stackoverflow.com/questions/68294992/how-to-get-input-from-console-in-qt) | [Console input with Qt Creator](https://stackoverflow.com/questions/1356328/console-input-with-qt-creator) – Abderrahmene Rayene Mihoub Aug 11 '23 at 09:59

1 Answers1

1

Run your application directly in terminal or choose "run in terminal" option in Qt Creator project's settings.

Mike
  • 519
  • 1
  • 4
  • 10