Batman
Batman

Reputation: 97

Qt console application ignores inputs (cin)

I just installed Qt Creator 13.0.2 and created my first console application (left all in default):

#include <QCoreApplication>
#include <QDebug>
#include <QString>
#include <QTextStream>

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

    qout << "Please enter your name: ";
    QString name = qin.readLine();
    qout << "Hello " << name;
}

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);
    
    do_qt();
    
    return a.exec();
}

When I run the application, this is what I get in the application output

21:41:39: Démarrage de C:\DEV\Qt\core_tuto\qt6-e3\qt6-e3\build\Desktop_Qt_6_7_2_MinGW_64_bit-Debug\qt6-e3.exe…
Please enter your name: Hello

It seems like the readLine() line is skipped.

Kit selection window (I left it in default):

enter image description here


I changed the option "Default for "Run in terminal"" to enabled (under edit->preferences->Build & Run):

enter image description here

Then, when I run the application, it starts blank (with no text displayed) in the terminal (in the bottom of the IDE). When I put a random string, it shows up in the second line like this:

xoxo Please enter your name: Hello xoxo

enter image description here

Upvotes: 1

Views: 195

Answers (1)

Swift - Friday Pie
Swift - Friday Pie

Reputation: 14614

Make sure you're running program in terminal. The Output window doesn't provide input stream capability so reading from stdin would silently fail. And for this particular scenario a qin.flush(); call might be required before using the streams.

Upvotes: 1

Related Questions