views:

1254

answers:

2

I'm running a very simple console app on Windows with Qt Creator.

When launching it, the dos console is openned, my output is displayed, but then the app terminates and the console immediately closes.

How can I make sure the console will stay open until the user presses a key ?

+1  A: 

Here are two solutions :

#include <QTextStream>
#include <QFile>
//#include <conio.h> // for getch()

int main(int argc, char *argv[])
{

    // JC and friends code

    // Qt Solution
    QTextStream Qin(stdin);
    forever
    {
        QString Line = Qin.readLine();
        if (!Line.isNull())
        {
            break;
        }
    }

    // conio solution
    //getch();
    return 0;
}

Both solutions tested with Qt Creator 1.2.1 on Windows Vista !

Hope it helps ;-)

Matthieu
I actually wish there would be a setting in Qt Creator ! Thx anyway !
Jérôme
+1  A: 

Since Qt Creator 1.3.0, it's much easier :

Go to the project tab (on the left) to edit the project's setting.

In the section Run Settings, clic on Show details and check the Run in Terminalcheckbox.

Thus, the application will be launched in a console window and the console window will wait until the enter key is pressed before closing.

No need to add some lines to the code anymore !

Jérôme