Console input with Qt Creator

2019-01-14 01:34发布

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 ?

标签: qt qt-creator
6条回答
Evening l夕情丶
2楼-- · 2019-01-14 01:59

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.

查看更多
太酷不给撩
3楼-- · 2019-01-14 02:08

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.

查看更多
Viruses.
4楼-- · 2019-01-14 02:09

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.

查看更多
Anthone
5楼-- · 2019-01-14 02:10

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!

查看更多
相关推荐>>
6楼-- · 2019-01-14 02:11

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!

查看更多
仙女界的扛把子
7楼-- · 2019-01-14 02:25

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 !

查看更多
登录 后发表回答