34
votes

I'm developping a very simple app on my Mac using QtCreator.

It's a console application and I want the user to enter its name, and then I display his name. Here is the code :

#include <iostream>

int main(int ArgC, char* ArgV[])
{
    char Name[1000];

    std::cout << "Type your name : ";
    std::cin >> Name;

    std::cout << "Hello " << Name << "\n";
    return 0;
}

When running this app with QtCreator, the string "Type your name :" is displayed in the 'Application Output' tab. But if I type some text and press the enter key, nothing is happening.

What's wrong ?

6

6 Answers

33
votes

I found a solution. With Qt Creator 1.3.0 (on Mac OS X), here is what I had to do :

  • Project->Run settings, check "Run in Terminal" (thanks Ropez)
  • Qt Creator->Preferences : Environnement : General : Terminal : I had to put the whole path to XTerm. For my config, I had to put /usr/x11/bin/xterm -e.

Now, everything is working fine !

10
votes

Go to Project -> Run settings, and make sure "Run in Terminal" is checked.

BTW:

std::cin >> Name;

is probably not what you want. It will read just a single token (typically only the first name). You should have a look at getline, or the string version.

2
votes

Jeromes solution is the proper one. Though I can give you a different alternative. In case you don't want to use X11 (for some reason anyhow) in the same position (QtCreator->Preferences->Environment:General:Terminal) you can give your path to the Terminal application like this: /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal

Enjoy!

2
votes

Solution for Windows.

In the .pro file add:

QT -= core gui
TEMPLATE = app
CONFIG += console

Go to Project -> Run settings, and make sure "Run in Terminal" is checked.

2
votes

I had the "Cannot start the terminal emulator 'xterm'" problem on Mac and fixed it by going to settings, Environment and clicking the "Reset" button next to the Terminal text field.

For some reason by default it just said "xterm -e" but after the reset it became an absolute path of "/usr/X11/bin/xterm -e".

My console app then ran fine.

1
votes

For Mac-based Qt 2.4.0, click on the Project vertical tab, which is located below the "Debug" along the same vertical line as Welcome, Edit, Design. In Target-> Run, make sure "Run in terminal" is checked!